Java Script Interview Questions and Answers

Difference between == and === in javascript?

The == checks value equal or not like
for example :

var a = 1;
var b = 1;
if(a == b){
    //true
}

from the above example a type is number and b type is also number.

var a = 1;
var b = '1';
if(a == b){
    //true
}

from the above example a is number and b is string. The type is different but the value is same.
Where as === check the value and type also.

var a = 1;
var b = '1';
if(a === b){}else{ // false}

the inner logic of === is like 

if(a == b && typeof(a) == typeof(b)){}

 

Difference between undefined and not defined in JavaScript?

In javascript when we are accessing/reading unknown variable (never declared before), then it will show error like Uncaught ReferenceError: xxx is not defined
where as undefined means, initilized a variable but not assigned a value like below

var a;
console.log(a); // undefined
and also 
var a = {name:"test"};
console.log(a.id) // undefined

from above both cases the result is undefined

What are the Data Types in  JavaScript ?

Below are the some data types in javascript
Number
String
Boolean
Object
Undefined

What is 'this' keyword in JavaScript?

'this' is refers to current execution object.

for examaple : 

var a = {
    fname : "John",
    lname : "Mike",
    fullname : function(){
        return this.fname + ' ' + this.lname;
    }
}
a.fullname() // output : John Mike

 

Global Variables Javascript ?

Global variables declared in outside of the function. We can access the variable any where in the page. Global variables have Global Scope.
For examaple :

var a = 10;
function b(){
    console.log(a); // output : 10
}

and also we can read the value using window object like below

console.log(window.a) // output : 10

 

Local Variables Javascript ?

Local Variables declared inside of the function, The scope will be within the function only. Local variables have functional scope.
For example :

function a(){
    var b = 0;
}
a();
console.log(b); // output : b is not defined

 

What is the output of the below code ?

var b = 10;
function a(){
    var b = 0;
}
a();
console.log(b);

 

The value of b is 10;
Because initial b is Global variable and the second b variable in the function, the second b variable have functional scope.
And one more thing in the function b variable is declared as a new variable. which will not override the global value.

What is "use strict" in javascript.

The "use strict" introduced in ECMAScript version 5. Which means the code is executed in strict mode. And "use strict" is declare in 1st line of the script or a function.
For examaple :

"use strict";
b = 10; // in this line it will return an error like b is not defined.

 

What is Lexical scope in Javascript.

Lexical scope nothing but nested group of functions, the inner functions have access parent function varibale.
For example :

functon a(){
    var d = 0;
    console.log(d); // output : 0
    console.log(e); // output : e is not defined
    function b(){
        var e = 10;
        console.log(d); // output : 0
        console.log(e); // output : 10
    }
}

 

Difference between const, let and var.

This is an advanced featuer from ES6. const, let and var are used for declaring a varibale only but they three have different scope.

const : When the value declared in const, the value never changed/modified.

const a = 10;
a = 15; // throw error.

let : When the value declared in let, It will have block scope.  Once a variable initilized with let, if we initilized with any type like let, var and const it will throw the error.

for(let i=0;i<3;i++){
    setTimeout(function(){
		console.log("i value is " + i);
	},100);
}

Output is :
i value is 0
i value is 1
i value is 2

var : When the value declared in var, It will have functional scope.

for(var i=0;i<3;i++){
    setTimeout(function(){
		console.log("i value is " + i);
	},100);
}


Output is :
i value is 3
i value is 3
i value is 3

What is JavaScript?

JavaScript is a lightweight programming language. JavaScript is a platform-independent,event-driven, interpreted client-side scripting language developed by Netscape Communications Corp. and Sun Microsystems.
JavaScript is a general-purpose programming language designed to let programmers of all skill levels control the behavior of software objects. The language is used most widely today in Web browsers whose software objects tend to represent a variety of HTML elements in a document and the document itself.
But the language is used with other kinds of objects in other environments. For example, Adobe Acrobat Forms uses JavaScript as its underlying scripting language to glue together objects that are unique to the forms generated by Adobe Acrobat.
Therefore, it is important to distinguish JavaScript, the language, from the objects it can communicate with in any particular environment.
When used for Web documents, the scripts go directly inside the HTML documents and are downloaded to the browser with the rest of the HTML tags and content.
The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.

How is JavaScript different from Java?

JavaScript was developed by Brendan Eich of Netscape; Java was developed at Sun Microsystems. While the two languages share some common syntax, they were developed independently of each other and for different audiences. Java is a full-fledged programming language tailored for network computing; it includes hundreds of its own objects, including objects for creating user interfaces that appear in Java applets (in Web browsers) or standalone Java applications. In contrast, JavaScript relies on whatever environment it's operating in for the user interface, such as a Web document's form elements.
JavaScript was initially called LiveScript at Netscape while it was under development. A licensing deal between Netscape and Sun at the last minute let Netscape plug the "Java" name into the name of its scripting language. Programmers use entirely different tools for Java and JavaScript. It is also not uncommon for a programmer of one language to be ignorant of the other. The two languages don't rely on each other and are intended for different purposes. In some ways, the "Java" name on JavaScript has confused the world's understanding of the differences between the two. On the other hand, JavaScript is much easier to learn than Java and can offer a gentle introduction for newcomers who want to graduate to Java and the kinds of applications you can develop with it.

What is the official JavaScript website?

This is a trick question used by interviewers to evaluate the candidate’s knowledge of JavaScript. Most people will simply say javascript.com is the official website.

The truth is- there is no official website for Javascript you can refer to. It was developed by Brendan Eich for Netscape. It was based on the ECMAScript language standard; ECMA-262 being the official JavaScript standard.

What’s relationship between JavaScript and ECMAScript?

ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.

What are the various datatypes in javascript?

Below are the some datatypes in javascript
Number
String
Boolean
Function
Object
Null
Undefined

What boolean operators does JavaScript support?

&&, || and !

What is negative infinity?

It’s a number in JavaScript, derived by dividing negative number by zero

Is it possible to check if a variable is an object?

Yes, it is possible to do so. The following piece of code will help achieve the same.

if(abc && typeof abc === "object") {
  console.log('abc is an object and does not return null value');
}

 

Can you explain what isNaN function does?

isNaN function will check an argument and return TRUE (1) if the argument does not seem to be a number.
NaN is a short form of Not a Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. When a string or something else is being converted into a number and that cannot be done, then we get to see NaN.