JavaScript Interview Questions

According to various websites, and StackOverflow off-course, there is a huge surge in demand for JavaScript developers. With frameworks like node.js, Angular, React , Vue.js gaining more popularity day by day, there is an increased demand for Javascript developers.

As such, there is also a surge in the number of test frameworks for Javascript- be it end to end testing or API Testing or Database testing. With frameworks like Jest, Mocha, Protractor, Cypress, Puppeteer, Supertest, Chakram etc., more and more QA’s are now moving to a JS stack.

As such, QA like me and a lot of others face this issue where they need to prepare for JavaScript questions in an interview. In this series I’ll try to cover a lot of such questions that are normally asked in the JavaScript interviews. I have compiled this list from a lot of online resources and I’ll list them at the bottom.

There will be both code and theory and basic Javascript language questions in this series. For code questions, I would suggest you to run them at least once on your machine on any IDE of your choice. I’ve tried to explain the code as much as I can.

 

  • What is Coercion in JavaScript?

Ans : In JavaScript, conversion between two different built in types is called coercion. Coercion comes in two forms – explicit and implicit.

 

This is an example of explicit coercion –


let a = '42';

let b = Number(a);

console.log(a);    //string

console.log(b);  //this is a Number

 

In case of implicit coercion,


let a = '42';

let b = a*1 // here a is implicitly coerced to Number

console.log(a);

console.log(b);

 

  • What is Scope in JavaScript?

Ans : In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function’s scoped variables.

A variable name has to be unique within the same scope. A scope can be nested inside another scope. If one scope is nested inside another, code inside the innermost scope can access variables from either scope.

 

  • Explain equality in JavaScript?

Ans: JavaScript has both strict and type–converting comparisons:

  • Strict comparison (e.g., ===) checks for value equality without allowing coercion
  • Abstract comparison (e.g. ==) checks for value equality with coercion allowed.

Some simple equality rules:

  • If either value (aka side) in a comparison could be the true or falsevalue, avoid == and use ===.
  • If either value in a comparison could be of these specific values (0"", or [] — empty array), avoid == and use ===.
  • In all other cases, you’re safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

 

  • What is a callback function with a sample example.

Ans : A callback function is a function that is passed to another function as an argument and is executed after some operation is performed.

 

  • What is the use of use strictin JavaScript?

Ans: The use strict literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake.

 

  • What is null and undefined in JavaScript?

Ans : JavaScript (and by extension TypeScript) has two bottom types: null and undefined. They are intended to mean different things:

  • Something hasn’t been initialized : undefined.
  • Something is currently unavailable: null.

undefined means a variable has been been declared but has not yet been assigned a value –

 

null on the other hand is an assignment value. It can be assigned to a variable as a representation of no value:

 

So, as you see, there is an obvious difference between these two. undefined in itself is a type while null is an object.

 

 

  • What is the difference between var, let and const in JavaScript?

Ans : In JavaScript, there are three ways of defining a variable – var, let and const. There are subtle but powerful differences between these three, and the major difference is in scoping.

 

var variables are function scoped, if defined inside a function. If not, they’re globally scoped.

let and const variable are block scoped – which mean they are available inside the block that they’re defined.