Best JavaScript Interview Questions and Answers

1) What is obejct in javascript?

Objects are simply variable but it store data in key value pairs (name and value separated by a colon).
example: var obj={name:’techroid’}

2) What is CROS? how does it use?

CROS is simply Cross-Origin Resource Sharing mean you can share resources (data, webpages,etc) from outside from the domain or in other word It is a mechanism supported in HTML5 that manages XMLHttpRequest access to a domain different.

3) What is DOCTYPE and why it is important?

Doctype is necessary to make few elements of html work properly. It is a best practice to include the doctype in your html page, if you are not including doctype then browser will work quirks mode(ehavior of older browsers).

4)Difference between typeof & instanceof?

typeof: By using typeof you get a string of the object type,
example: var obj={};
console.log(typeof obj);
result// ‘object’

instanceof: by using instanceof you are comparing the type, specifically if the property of a constructor is in the objects prototype chain.

example:
var obj=function(){}
var newobj= new obj();
console.log(newobj instanceof obj);
result// true

5)What is prototype ?
Every object in Javascript has a prototype. When a messages reaches an object, JavaScript will attempt to find a property in that object first, if it cannot find it then the message will be sent to the object’s prototype and so on. This works just like single parent inheritance in a class based language

6) Does javascript have classes?
Although the statement “JavaScript has no classes” is correct, JavaScript is object-oriented language, it isn’t a class-based language it’s a prototype-based language.

7)What will be the output of the following code and why?
(function() {
console.log(1);
setTimeout(function(){console.log(2)}, 1000);
setTimeout(function(){console.log(3)}, 0);
console.log(4);
})();

8)What will be the output of the following code and why?
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 == 0.3);

9) what will be the output of the following code and why ?

var object={}
var object2={}
if(object)
cosole.log(object2)
or
console.log(object===object)

10) create an object using function constructor ?

function obj(){
this.name=’Sultan’,
this.age=’25’
}
var object=new obj();
will this work or not
obj.gender=’Male’

11)What will be the output of the following code?
var a={},
b={key:’b’},
c={key:’c’};

a[b]=123;
a[c]=456;

console.log(a[b]);

12) What is currying in JS ?
Currying means first few arguments of a function is pre-processed and a function is returned. The returning function can add more arguments to the curried function.

13) what is web worker?
A web worker is a script that runs in the background (i.e., in another thread) without the page needing to wait for it to complete. The user can continue to interact with the page while the web worker runs in the background. Workers utilize thread-like message passing to achieve parallelism.

14) List some design patterns in Javascript?
Module
Prototype
Observer
Singleton

15) How to create class in Javascript?
there are many ways you can create class in JavaScript, I am listing out few here,

1)Using function
function Car(type) {
this.type = type;
this.color = “red”;
this.getInfo = getCarInfo;
}
function getCarInfo;() {
return this.color + ‘-‘ + this.type ;
}

2)Object literals
var Car= {
type: “BMW”,
color: “white”,
getInfo: function () {
return this.color + ‘-‘ + this.type ;
}
}
console.log(apple.getInfo());

3)using singleton

var Car= new function() {
this.type = “Maruti”;
this.color = “red”;
this.getInfo = function () {
return this.color + ‘-‘ + this.type ;
};
}
console.log(apple.getInfo());

16) Explain what is bind?

Bind in javascript is a method — Function.prototype.bind . bind is a method. It is called on function prototype. This method creates a function whose body is similar to the function on which it is called but the ‘this’ refers to the first parameter passed to the bind method.

17) How closure work?
A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result. Or

A closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns (as if a ‘stack frame’ were allocated on the heap rather than the stack!).
The following code returns a reference to a function:

function sayHello2(name) {
var text = ‘Hello ‘ + name; // Local variable
var say = function() { console.log(text); }
return say;
}
var say2 = sayHello2(‘Bob’);
say2();

The above code has a closure because the anonymous function function() { console.log(text); } is declared inside another function, sayHello2() in this example. In JavaScript, if you use the function keyword inside another function, you are creating a closure.