JavaScript Interview Questions

A Guide to most asked JavaScript interview questions.

  1. What are the differences between variables created using let, var and const?
  2. What's the difference between a variable that is: null, undefined or undeclared?
  3. What's the difference between == and ===?
  4. What are the differences between cookies, local storage, and session storage?
  5. What are data types in JavaScript?
  6. What is Ecmascript? Differences between ES5 and ES6 ?
  7. What is hoisting in JavaScript?
  8. What is the strict mode mode in JavaScript?

1. What are the differences between variables created using let, var and const? #

let and const are came with ES6 (Ecmascript 2015). Why we need them? What is the difference between them and var?

  • undefined is a default value for var and let.
  • var can be function scoped and globally scoped.
  • var variables can be re-declared and updated.
  • var and letcan be declared without being initialized, const must be initialized during declaration.
  • let variables can't be re-declared but updated.
  • let and const are block scoped.
  • const variables are immutable. They can't be re-declared or updated.
Variable Declaration #
var fruit; // variable declaration
console.log(fruit); // undefined
Global Scope #
var note = "Buy a bread and some cookies!"; // This is a global scope variable.

function readMyNote(){
console.log(note);
}
readMyNote(); // Buy a bread and some cookies!
Function Scope #
function readMyNote(){
var note = "Buy a bread and some cookies!"; // This is a function scope variable.
return note;
}
readMyNote(); // Buy a bread and some cookies!
console.log(note); // Uncaught ReferenceError: note is not defined
Block Scope #
var a = 10;
{
let b = 20; // This is block scope.
console.log(b); // 20
}
console.log(a); // 10
console.log(b); // Uncaught ReferenceError: note is not defined

2. What's the difference between a variable that is: null, undefined or undeclared? #

  • A variable is undeclared if it's not declared. This variable is not exist.
  • A variable is undefined if it hasn't been assigned a value. This variable is exist.
  • Null is a data type and a type of object.
Undeclared #
console.log(x); // Uncaught ReferenceError: r is not defined
Undefined #
let y;
console.log(y); // undefined
Null #
let z = null;
console.log(z); // null

3. What's the difference between == and ===? #

  • Double equals (==) is a comparison operator.
  • Triple equals (===) is a strict equality comparison operator.
  • Triple equals (===) checks that two values are same or not. (eg: string vs string). The values must be equal in type as well.
0 == false   // true
0 === false // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

4. What are the differences between cookies, local storage, and session storage? #

  • Cookies can be stored in browser and server.
  • If you want to control from backend use cookies.
  • Expires.

 

  • Local Storage can be stored only in browser.
  • Never expire until deleted.
  • No data transfer to backend.

 

  • Session Storage Expires when you closed the tab or window.
  • No data transfer to backend.
Cookies Local storage Session storage
Capacity 4KB 10MB 5MB
Accessible from Any window Any window Same tab
Expiration Manually set Never On tab close
Storage location Browser and server Browser only Browser only
Sent with requests Yes No No
Blockable by users Yes Yes Yes
Editable by users Yes Yes Yes

5. What are data types in JavaScript? #

There are total 7 data types in JavaScript.

  1. Number
  2. String
  3. Boolean
  4. Object
  5. Undefined
  6. Null
  7. Symbol

6. What is Ecmascript? Differences between ES5 and ES6 ? #

ECMAScript is a trademarked scripting language specification that is defined by ECMA International.

We have new features like;

  • Template Literals
  • Arrow functions
  • Scoping
  • Internationalization
  • Localization

and more.


7. What is hoisting in JavaScript? #

Variables, classes, and functions can be used before they're declared. This behavior is called hoisting.


8. What is the strict mode in JavaScript? #

It's a way to better error checking in your code. You can enable strict mode by adding use strict at the beginning of a file.

Previous Post

Modern CSS Reset

This most modern CSS reset yet is the first file you have to copy to your project

Read article
Next Post

JavaScript Format Numbers as Currency Strings

As you know, JS has built-in objects. The ES6 Internationalization API helps us format numbers as currency strings.

Read article