Top 10 JavaScript Interview Question

1. Truthy vs Falsy

falsy  => false, '', "", 0, -0, 0n, NaN, null, undefined.
truthy => anything that is not mentioned above.

2. null vs undefined

let name;
console.log(name); // undefined
let name = null;
console.log(name); // null

3. Double equal (==) vs Triple equal (===)

const a = 20;
const b = '20'
console.log(a == b); // true
console.log(a === b); // false

4. Scope, block scope

var a  = 20; // This is global scopefunction name(){
let name = 'Hossain'; // This is block scope
return name;
}
console.log(name());

5. Closure

function outer(){
var fName = 'Hossain ';
function inner(){
var lName = 'Rabbi'
console.log(fName + lName);
}
return inner;
}
outer()();

6. Find the largest element of an array

const array = [10, 25, 3, 65, 14, 55, 36];let max = array[0];
for(let i = 0; i < array.length; i++){
const element = array[i];
if(element > max){
max = element
}
}
console.log(max); // 65

7. Remove duplicate item from an array

const array = [10, 25, 10, 3, 65, 3, 14, 55, 36];let uniqueArray = [];
for(let i = 0; i < array.length; i++){
const element = array[i];
const index = uniqueArray.indexOf(element);

if(index === -1){
uniqueArray.push(element);
}
}
console.log(uniqueArray); // [10, 25, 3, 65, 14, 55, 36]

8. What is Recursion

function power(x, n) {
return (n == 1) ? x : (x * power(x, n - 1));
}
power(3, 2);

9. What is callback function

function print(name, callback) {
callback(name)
}
print('Hossain', (name) => {
console.log('My Name is ' + name); // My Name is Hossain
});

10. What is DOM?

Front-End Developer