Top 10 JavaScript interview questions

MariamChowdhury
3 min readMay 8, 2021

This article will discuss the top 10 questions that every js developer needs to know to excel in any interview.

Example of truthy values and false values

Any number is truthy other than 0. Any string is truthy other than an empty string. A white space inside a string, 0 inside a string are examples of truthy values. Undefined, null, NaN is falsy values. Empty array and objects are truthy.

How to get undefined?

If we declare a variable but don’t put any values, then it's undefined. If we call a function, but that function does not return anything, write return keyword but don’t tell explicitly what to return, if we want a property from an object which does not exist in the object, if we set the value of a variable undefined (not recommendable at all) then we get undefined.

Difference between double equal and triple equal

Double equal checks the value only, but triple equal checks value and data type both.

What is hoisting?

If we declare a variable inside a scope using let/const, that variable is not accessible outside of the scope. But if we use let, then we can access that variable from any place in the code. It is because of hoisting. In summary, hoisting takes the variable to its parent element to be accessible from any place.

How to find the largest element in an array?

var ara=[1,22,39,2,19,98,5]
var max=ara[0]
for(var i=0;i<ara.length;i++){
var element=ara[i]
if(element>max){
max=element;
}
}
console.log('Highest is:',max);

Remove duplicate item from an array

var nums = [3, 7, 11, 8, 3, 2, 11, 7, 8, 3, 6, 5];
var unique = [];
for(var i = 0; i<nums.length; i++){
var element = nums[i];
if(unique.indexOf(element) == -1){
unique.push(element);
}
}
console.log("Unique array is: ", unique);

Reverse a string

var str = "I am becoming a web dev!";
var reverse = '';

for(var i = 0; i<str.length; i++){
var element = str[i];
reverse = element + reverse;
}
console.log(str);
console.log(reverse);

Swap variables

var a = 5;
var b = 7;
console.log("before swap: a =", a,"b =", b);
// solution 1
var temp = a;
a = b;
b = temp;
console.log("after swap: a =", a,"b =", b);
// solution 2
var x = 5;
var y = 7;
console.log("before swap: x =", x,"y =", y);
[x,y] = [y,x]
console.log("after swap: x =", x,"y =", y);
//solution 3
var p = 5;
var q = 7;
console.log("before swap: p =", p,"q =", q);
p = p + q;
q = p - q;
p = p - q;
console.log("after swap: p =", p,"q =", q);

Sum of numbers

var nums = [23, 54, 1, 3, 54, 76, 45];

var sum = 0;
for(var i = 0; i < nums.length; i++){
var element = nums[i];
sum = sum + element;
}
console.log("the total is: ", sum);

Check if a number is prime or not

function isPrime(num){
for(var i = 2; i<num; i++){
if(num % i == 0){
return false;
}
}
return true;
}
console.log(isPrime(7));

--

--