JavaScript Interview Questions

1. What are the different data types present in javascript?

To know the type of a JavaScript variable, we can use the typeof operator.

Primitive types

String – It represents a series of characters and is written with quotes. A string can be represented using a single or a double quote.

 var str = "Vivek Singh Bisht"; //using double quotes
 var str2 = 'John Doe'; //using single quotes

Number – It represents a number and can be written with or without decimals.

var x = 3; //without decimal
var y = 3.6; //with decimal

BigInt – This data type is used to store numbers which are above the limitation of the Number data type. It can store large integers and is represented by adding “n” to an integer literal.

var bigInteger =  234567890123456789012345678901234567890;

Boolean – It represents a logical entity and can have only two values : true or false. Booleans are generally used for conditional testing.

var a = 2;
var b =  3;
var c =  2;
(a == b) // returns false
(a == c) //returns true

Undefined – When a variable is declared but not assigned, it has the value of undefined and it’s type is also undefined.

ar x; // value of x is undefined
var y = undefined; // we can also set the value of a variable as undefined

Null – It represents a non-existent or a invalid value.

var z = null;

Symbol – It is a new data type introduced in the ES6 version of javascript. It is used to store an anonymous and unique value.

var symbol1 = Symbol('symbol');

typeof of primitive types :

typeof "John Doe" // Returns "string"
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof 234567890123456789012345678901234567890n // Returns bigint
typeof undefined // Returns "undefined"
typeof null // Returns "object" (kind of a bug in JavaScript)
typeof Symbol('symbol') // Returns Symbol

Non-primitive types

Primitive data types can store only a single value. To store multiple and complex values, non-primitive data types are used.

Object – Used to store collection of data.

Example:

// Collection of data in key-value pairs

var obj1 = {
   x:  43,
   y:  "Hello world!",
   z: function(){
      return this.x;
   }
}
      
// Collection of data as an ordered list
      
var array1 = [5, "Hello", true, 4.1];   

2. Explain Hoisting in javascript.

Hoisting is a default behaviour of javascript where all the variable and function declarations are moved on top.

javascript

This means that irrespective of where the variables and functions are declared, they are moved on top of the scope. The scope can be both local and global.

hoistedVariable = 3;
console.log(hoistedVariable); // outputs 3 even when the variable is declared after it is initialized	
var hoistedVariable;
hoistedFunction();  // Outputs " Hello world! " even when the function is declared after calling

function hoistedFunction(){ 
  console.log(" Hello world! ");
}
// Hoisting takes place in the local scope as well
function doSomething(){
  x = 33;
  console.log(x);
  var x;
}

** Variable initializations are not hoisted, only variable declarations are hoisted:

var x;
console.log(x); // Outputs "undefined" since the initialization of "x" is not hoisted
x = 23;

**Note – To avoid hoisting, you can run javascript in strict mode by using “use strict” on top of the code:

"use strict";
x = 23; // Gives an error since 'x' is not declared
var x; 

3. Difference between “ == “ and “ === “ operators.

Both are comparison operators. The difference between both the operators is that,“==” is used to compare values whereas, “ === “ is used to compare both value and types.

Example:

var x = 2;
var y = "2";
(x == y)  // Returns true since the value of both x and y is the same

(x === y) // Returns false since the typeof x is "number" and typeof y is "string"

4. Explain Implicit Type Coercion in javascript.

Implicit type coercion in javascript is automatic conversion of value from one data type to another. It takes place when the operands of an expression are of different data types.

String coercion

String coercion takes place while using the ‘ + ‘ operator. When a number is added to a string, the number type is always converted to the string type.

Example 1:

var x = 3;
var y = "3";
x + y // Returns "33" 

Example 2:

var x = 24;
var y = "Hello";
x + y   // Returns "24Hello";

To learn basic To advanced of JavaScript. Please Hit like and subscribe my channel

Click Here To watch Video

Leave a Reply

Your email address will not be published. Required fields are marked *