JavaScript Interview Questions and Answers

Abdul Hannan
4 min readMay 8, 2021

JavaScript is the most demanded programming language in recent tech world. Basically, it was created to build dynamic website. Now it is also used as server-side language to manage website server. So gradually its demand is increasing almost in all technology related company. That’s why every year they hire lots of JavaScript developer. If you think you are good JavaScript developer you can jump for the job and before you start jumping you must know some JavaScript interview questions and their answers. Lets explore some JavaScript important questions and answers.

1. Double equal ( = = ) vs triple equal ( = = = )

This is the most common interview question. This is used to compare 2 items and the main different between both are double equal will check values. Example:

let first = 0;

let second = “0”;

if(first == second) {

console.log(‘condition is true’);

}

else {

console.log(‘condition is false’);

}

Triple equal will check both values and type. Example:

let first = 0;

let second = “0”;

if(first === second) {

console.log(‘condition is true’);

}

else {

console.log(‘condition is false’);

}

You can see here returns false because here value is same but first value is number and second value is string.

2. What is closure?

If one function is called inside another function or return another function it will create a close environment then returned function access its outside variable and that variable created every return value will create different value. That is closure.

function stopwatch() {

let count = 0;

return function() {

count++;

return count;

}

}

const clock1 = stopwatch();

console.log(clock1());

console.log(clock1());

console.log(clock1());

3. What is truthy and falsy values?

If you set any number in the variable except 0 that is true value and if variable is 0 that is false in javascript. For example:

const age = 3;

if(age > 0) {

console.log(“condition is true”);

}

else {

console.log(“condition is false”)

}

On the other hand if I set empty string inside the variable “” the result will be false. That means there is nothing inside the string always the result will be false.

const name = “”;

if(name) {

console.log(“condition is true”);

}

else {

console.log(“condition is false”)

}

4. Null vs Undefined:

Undefined can be explain in numerous ways. If you declare a function but you don’t return anything the result will be undefined and if you don’t pass parameter by default, you will get the result undefined. For example:

function add (num1, num2) {

console.log(num1, num2);

}

const result = add(13);

console.log(result);

on the other hand, null means first, there was a value but now there is nothing which is set by developer. So null is nothing and not existence of the value.

5. Find largest element:

If you want to find largest element from the array there is lots of way to find out. First you can eliminate lowest number from the array one by one but don’t you think that is too difficult to find out largest number from the array because if you have thousands of numbers inside the array what will you do? First and last solution is doing some programming with the array. For example:

var marks = [46, 84, 12, 65, 98, 99, 78];

var max = marks[0];

for (let i = 0; i < marks.length; i++) {

const element = marks[i];

if(element > max) {

max = element;

}

}

console.log(max);

6. Remove duplicate item

If you have a big and long collection of array and you want to remove duplicate items or repeated item from the array you can do it one by one just start from beginning of the array and eliminate repeated items. Don’t you think that is really difficult? There is a JavaScript solution just follow this for loop function:

var name = [2, 5, 6, 5, 1, 9, 8, 9, 6, 10, 12, 10];

var uniqueName = [];

for (let i = 0; i < name.length; i++) {

const element = name[i];

const index = uniqueName.indexOf(element);

if(index == -1) {

uniqueName.push(element);

}

}

console.log(uniqueName);

7. Count Number of string words:

If you want to count the number of word from string you can do it one by one or you can do it by doing some coding. For example:

var speech = “i am a really good person. i don’t snore at night”;

var count = 0;

for (var i = 0; i < speech.length; i++) {

var char = speech[i];

if(char == “ “ ) {

count++;

}

}

count++;

console.log(count);

8. Reverse a string:

Suppose you are speaking a line “I am a good person” if you reverse the line how it look like? Sometimes you need to do this in your coding journey. For example:

function reverseString(str) {

ver reverse = “”;

for (let i = 0; i < str.length; i++) {

const char = str[i];

reverse = char + reverse;

}

return reverse;

}

var statement = “hello i am not a alien”;

ver forAlien = reverseString(statement);

console.log(forAlien);

9. Declare Function and Arrow Function:

Arrow function ES6 latest function declaration process. Lets learn how to declare normal function and arrow function. Normal function example:

function doubleIt(num) {

return num * 3;

}

const result = doubleIt(5);

console.log(result);

and here I declare same function with arrow style:

const doubleIt = num => num * 3;

const result = doubleIt(5);

console.log(result);

10. How JavaScript works?

When you type a javascript code it passed through a compiler. Compiler translates the code into bytecode. Every web browser has different name javascript compiler engine. Google has V8 javascript engine which is most popular. Firefox hax spiderMonkey javascript compiler engine.

--

--