JavaScript Interview Questions and Solutions

Top 10 Questions and Solutions

Jahid Hasan Juyel
5 min readMay 8, 2021

01. What is the difference between null & undefined in JavaScript?

The value nullis an assignment value that represents the absence of any object value. It means that a variable of no value:

Example:

var testVar = null;
alert(testVar); //shows null
alert(typeof testVar); //shows object

On the other hand A variable that has not been assigned a value is of type undefined. It means a variable has been declared but has not yet been assigned a value.

Example:

var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined

02. What is the difference between double equal (==) vs triple equal (===)?

JavaScript provides three different value-comparison operations:

  • === Strict Equality Comparison (“strict equality”, “identity”, “triple equals”)
  • == Abstract Equality Comparison (“loose equality”, “double equals”)

Strict Equality considers values equal that have the same type.

  1. undefined === undefined, null === null,
  2. NaN === nothing including itself,
  3. Primitive [Number|String|Boolean] === primitive value equal,
  4. to self (+0 === -0)
  5. Two objects [Array|Object|Function] === Only self (same exact entity)

Example:

var num = 0;
console.log(num === num); // true

Loose Equality considers values equal that have the same type.

  1. If both values have the same type: compare with ===.
  2. undefined == null
  3. number and string: string => number and compare
  4. boolean and non-boolean => non-boolean to number and compare
  5. string or number => an object: convert object to primitive and comparison.

Example:

const num = 0;
const str = '0';
console.log(num == str); // true

03. How do JavaScript closures work?

A closure is the combination of a function bundled together. Closures are created every time a function is created, at function creation time.

Example:

function makeFunc() {
var name = 'Mozilla';
function displayName() {
alert(name);
}
return displayName;
}

var myFunc = makeFunc();
myFunc();

Working Steps:

  • Whenever a function is declared in JavaScript closure is created.
  • Returning a function from inside another function is the classic example of closure, because the state inside the outer function is implicitly available to the returned inner function, even after the outer function has completed execution.
  • Whenever you use eval() inside a function, a closure is used. The text you eval can reference local variables of the function, and in the non-strict mode, you can even create new local variables by using eval('var foo = …').
  • When you use new Function(…) inside a function, it does not close over its lexical environment: it closes over the global context instead. The new function cannot reference the local variables of the outer function.
  • A closure in JavaScript is like keeping a reference (NOT a copy) to the scope at the point of function declaration, which in turn keeps a reference to its outer scope, and so on, all the way to the global object at the top of the scope chain.
  • A closure is created when a function is declared; this closure is used to configure the execution context when the function is invoked.
  • A new set of local variables is created every time a function is called.

04. What is Scope?

Scope is a function that serves as a closure in JavaScript. In a Scope, a variable defined exclusively within the function cannot be accessed from outside the function or within other functions.

Example:

function exampleFunction() {
var x = "declared inside function"; // x can only be used in exampleFunction
console.log("Inside function");
console.log(x);
}

console.log(x); // Causes error

05. Find the largest element of an array.

Here you can use loop like this-

Example:

var arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (largest < arr[i] ) {
largest = arr[i];
}
}
console.log(largest);

Or you can use the Math.max() function returns the largest of the zero or more numbers given as input parameters

console.log(Math.max(1, 3, 2));
// expected output: 3

06. Reverse a string

The reverse() method reverses an array. The first array element becomes the last, and the last array element becomes the first.

Example:

const array1 = ['one', 'two', 'three'];
console.log(array1.reverse());
// expected output: "reversed:" Array ["three", "two", "one"]

07. Check whether a number is a Prime Number or not.

Prime numbers are whole numbers greater than 1, that have only two factors 1 and the number itself. Prime numbers are divisible only by the number 1 or itself.

Example:

function isPrime(num) {

if (num === 2) {
return true;
} else if (num > 1) {
for (var i = 2; i < num; i++) {

if (num % i !== 0) {
return true;
} else if (num === i * i) {
return false
} else {
return false;
}
}
} else {
return false;
}

}

console.log(isPrime(121));

08. What is Arrow function?

An arrow function expression is a compact alternative to a normal function expression, but is limited and can’t be used in all situations.

Differences & Limitations:

Example:

// Traditional Function
function (a, b){
return a + b + 100;
}

// Arrow Function
(a, b) => a + b + 100;

// Traditional Function (no arguments)
let a = 4;
let b = 2;
function (){
return a + b + 100;
}

// Arrow Function (no arguments)
let a = 4;
let b = 2;
() => a + b + 100;

09. What is DOM (Document Object Model)

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can connect to the page.

A Web page is a document. This document can be either displayed in the browser window or as the HTML source. The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript.

For example, the standard DOM specifies that the querySelectorAll method in the code below must return a list of all the <p> elements in the document:

const paragraphs = document.querySelectorAll("p");
// paragraphs[0] is the first <p> element
// paragraphs[1] is the second <p> element, etc.
alert(paragraphs[0].nodeName);

10. What is an API?

Application Programming Interfaces (APIs) are constructs made available in programming languages to allow developers to create complex functionality more easily. They abstract more complex code away from you, providing some easier syntax to use in its place.

--

--