JavaScript Interview Questions and Answers

Javascript Interview Questions and Answers are designed to provide a better understanding of general questions regarding JavaScript programming and its functionality, JavaScript Fundamentals , JavaScript Data Structures, Advanced JavaScript concepts etc. These questions will often link to more detailed information. Here are some of the Advanced JavaScript Interview Questions and Answers for freshers and experienced.

What is memoization?

Memoizing in simple terms means that memorizing or storing in memory. Sometimes, a function can become expensive to call multiple times in a program. But there's a way you can optimize such functions and make them execute much faster is called "caching" . Memoization is a way for your function to remember (cache) the results. The concept of Memoization in Javascript is backed by two main sub-concepts: Closure and High order function. In JavaScript, closures are generated every time a function is created and it allows you access to the domain of the outer function from the inner function. A high order function accepts another function as an argument or returns a function as its output. This technique is specially useful when it comes to dynamic programming . A memoized function is usually faster because if the function is called subsequently with the previous value(s), then instead of executing the function, would be fetching the result from the cache .

What is Currying in javascript

Currying is an advanced javascript technique of working with functions. It breaks down a function to takes more than one arguments into a series of functions that each take only one argument. In other words, it is a transformation of functions that translates a function from callable as f(x, y, z) into callable as f(x)(y)(z) . example
function curryThis(inArgs) { return function doCurry(...args) { if (args.length >= inArgs.length) { return inArgs.apply(this, args); } else { return function(...args2) { return doCurry.apply(this, args.concat(args2)); } } }; }
function volume(x, y, z) { return x * y * z; } let curriedVolume = curryThis(volume);
console.log(curriedVolume(2, 4, 6) ); //callable normally , output: 48 console.log(curriedVolume(2)(4,6) ); //currying of 1st arg , output: 48 console.log(curriedVolume(2)(4)(6) ); //full currying , output: 48
From the above example you can understand that instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.

What is Object Destructuring?

Object destructuring is a JavaScript expression that allow you to unpack values from a data collection (object, array, properties etc.) and maps and set them into new, distinct variables without having to iterate over the data or access its keys explicitly. example
var students = { // Object to destructure fName: 'Jon', lName: 'Doe', age: 12, yob:2000 };
// Destructuring the object into variables var { fName, age} = students; console.log( fName, age); //output: Jon 12

Does JavaScript support automatic type conversion?

Yes! Javascript support automatic type conversion . It's usually called type coercion. JavaScript Type Coercion means that when the operands of an operator are different data types, one of them will be converted to an equivalent value of the other operand's type .
boolean == integer
In the above code the boolean operand will be converted to an integer: false becomes 0, true becomes 1. Then the two values are compared. example
console.log("Auto: " + 50); //return Auto: 50
Above code converts 50 (a number) to string, done by the + operator .
Advanced javascript interview questions and answers

What is the output of the following code?

console.log(0.2 + 0.3 === 0.5);
Above code return false . Since the floating point numbers are encoded in binary format , the addition operations on them lead to rounding errors . So, the comparison of floating points doesn't give expected results because of float point math problem .

What is Same Origin Policy?

The Same Origin Policy (SOP) is a security measure standardized among browsers. It is needed to prevent Cross-Site Request Forgery (CSRF). The "Origin" mostly refers to a "Domain". Same Origin Policy prevents different origins (domains) from interacting with each other, to prevent attacks such as CSRF (Cross Site Request Forgery) through such requests, like AJAX. In other words, the browser would not allow any site to make a request to any other site. Without Same Origin Policy , any web page would be able to access the DOM of other pages. This SOP (Same Origin Policy) exists because it is too easy to inject a link to a javascript file that is on a different domain. This is actually a security risk ; you really only want code that comes from the site you are on to execute and not just any code that is out there.

How are JavaScript and ECMA Script related?

Javascript was originally created at Netscape Communications Corporation , and they wanted to standardize the language. So, they submitted the language to the ECMA for standardization. Because of trademark issues with the name Javascript, and the standard became called ECMAScript . The core functionality of Javascript are based on the ECMAScript specifications, but Javascript also has other additional features that are not in the ECMA standard . There are languages other than JavaScript (ActionScript by Adobe Flash and JScript by Microsoft) that also implement the ECMAScript Standard as their core.
Javascript interview questions and answers

What is prototypal Inheritance?

A prototype is a working object instance. An object inherits properties from another object via the prototype linkage is called Prototypal Inheritance. This means that objects inherit directly from other objects. Using this functionality, you can reuse the properties or methods from one JavaScript object to another through a reference pointer function.

What is negative infinity in JavaScript?

Negative infinity is something that is lower than any other number . This means that no other number is lesser than this value. In JavaScript negative infinity is a constant value which is used to represent a value which is the lowest available .
var x = 500; x.NEGATIVE_INFINITY;
returned "undefined"

Explain WeakSet in javascript

JavaScript WeakSets are a collection of Objects where each object can only appear once in the set, similar to Set. It contains unique objects only. If there is no reference to a stored object, they are targeted to garbage collection . WeakSet can be useful in a situation where you want to "tag" objects without actually mutating them. This means that it is useful for security and validation reasons. If you want to be able to isolate a piece of JavaScript, WeakSets allow you to tag an object to indicate it belongs to a special set of object. The only drawback is that WeakSets are not enumerable . You can't iterate over a list of entries, probably because this would likely "touch" those entries and so defeat the purpose of WeakSet concept.

What are arrow functions?

Arrow functions introduced in ES6 and it aimed to address and resolve several usual pain points of traditional function expressions. They utilize the => syntax , which looks like an arrow. Arrow functions allow you to write shorter function syntax.
let sum = (x, y) => x + y; console.log(sum(20, 45));//output is 65

Is same as,

let sum = function (x, y) { return x + y; }; console.log(sum(20, 45)); //output is 65
Arrow functions are more like function statements , except that they bind the this to the parent scope. The this keyword inside an arrow function , does not refer to the object calling it rather inherits its value from the parent scope.

What is the use of the "debugger" keyword?

The debugger keyword in JavaScript set the breakpoint through the code itself. It stops the execution of JavaScript, and calls the debugging function if it is available else no action is performed. Continue....... JavaScript Interview Questions (Part2)