Most 10 popular JavaScript interview questions.

Md Mehedy Hassan
5 min readMay 8, 2021
interview questions

Questions Summary

1. True vs False

2. Null vs Undefined

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

4. setTimeout();

5. JavaScript event loop

6. How to defined largest element of an array?

7. Remove duplicate item from an array.

8. Factorial with for loop

9. Why we use let?

10. Why we use const?

1. True vs False

True and False are usually the JavaScript Booleans type. Today we will know which ones are called true and which ones are called false.

True: If the value of a variable is declared and if we apply condition to them then its value will show true.

example:

const number = 2;
if(number){
console.log("true")
}
else{
console.log("false")
}
// output: true

False: If the value of a variable is 0, undefined, “”, null it will show false. A lot of times if the value is not set then this way will show false.

example:

const number = 0;
if(number){
console.log("true")
}
else{
console.log("false")
}
// output: false

2. Null vs Undefined

The path between null and undefined is:

Null: If we declare the value of a variable as direct null, we call it null. Null means no value exists. In many cases we use null as default.

example:

const mango = null;
console.log(mango)
// output: null

Undefined: If we do not set a value in a variable, it is called undefined. If we call a variable whose value we have not declared, it will show us undefined.
Even if we use direct undefined in a variable, it will show undefined. But it is not right to do so.

example:

const mango = undefined;
console.log(mango)
// output: undefined
let banana;
console.log(banana)
// output: undefined

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

Double equal: Double equals means that the two will match if they are the same. If the value of one variable is typeof number 7 and another typeof string is “7”. Then they will be counted as equal. These are usually expressed by double equals.

example:

const first = 7;
const second = "7";
if(first == second){
console.log("true")
}
else{
console.log("false")
}
// output: true

Triple equal: Triple equals means if the values ​​of the two variables are the same to the same. That is, only if the values ​​are the same. If for some reason one of the values ​​has a typeof number and the other has a value typeof string then it will not happen. Both must be the same. If it is different, it will be false.

example:

const first = 7;
const second = "7";
if(first === second){
console.log("true")
}
else{
console.log("false")
}
// output: fasle
const mango = 7;
const banana = 7;
if(mango === banana){
console.log("true")
}
else{
console.log("false")
}
// output: true

4. setTimeout();

We use “setTimeout” when we want to show a value after all.
Usually JavaScript works in a serial way. However, we can break this serial through setTimeout. When we call a function or something else, it will come serially. However, if you call through setTimeout, it may not be serial. setTimeout means that this work will be at the end of everyone. This work will be done when all the work is done. A lot of times when we work on a very big project we can use it.

example:

function last(){
console.log(1);
}
console.log(2);
setTimeout(last);
console.log(3);
console.log(4);

5. JavaScript event loop

The way JavaScript works is that they first have to do everything in a serial way and if a value is put in the timeout then that value will come at the end of it all. Again, if there is a callback, it will be later. There are a few such as OnClick onLoad Ondone after clicking on these. This is how JavaScript works. An example is shown below through an image.

example:

6. How to defined largest element of an array?

If we want to know who is doing the best result in a test, we have to find out the result of everyone first. And we have to find out who got higher marks than him. And it will take a lot of time to do this. But if we use JavaScript, we can easily find out.

example:

const marks = [ 25, 65, 85, 95,70, 58, 12, 95, 84, 96, 35, 36, 50]
let max = marks[0];
for (let i = 0; i < marks.length; i++) {
const element = marks[i];
if(element > max){
max = element;
}
}
console.log(max);
// output: 96

7. Remove duplicate item from an array.

If there is a time when this number is being added again and again but we want a number to be used only once. Then if it is less then we can calculate but if it is a little more then it will cost us a lot of time. Which will take us a long time. But if we want to get this job out through JavaScript, we can get it out very easily. We can define a string in this way.

example:

const number = [ 15, 16, 12, 15, 16, 14, 19, 15, 18, 16, 15, 12, 11]
let uniqueNumber = [];
for (let i = 0; i < number.length; i++) {
const element = number[i];
let index = uniqueNumber.indexOf(element)
if(index == -1) {
uniqueNumber.push(element)
}
}
console.log(uniqueNumber)
// output: [ 15, 16, 12, 14, 19, 18, 11 ]

8. Factorial with for loop

A factorial is the product of a number and all the numbers before it. That is, if we want to find the factorial of 2, then we have to write 1X2. If we want to find the factorial of 3 then it will be 1X2X3. If we want to do a factorial bar of 4 then we have to write 1X2X3X4. If we express the factorial through for loop, we have to do it this way. We will now see how to do this very easily with For Loop.

example:

let factorial = 1;
for (let i = 1; i <= 10; i++){
factorial = factorial * i
console.log(factorial)
}
// output: (last factorial) 3628800

9. Why we use let?

Let is a variable whose value can be a chance. And if this variable is not returned from the function, it cannot be worked from outside. This variable has been around since the advent of ES6, before which the variable was simply declared as a var. Due to which there were many problems but car var variable is used as global variable. And who wouldn’t use this variable once.

example:

let name = "Mehedy";
name = "Hassan";
console.log(name);
// output: Hassan

10. Why we use const?

Const is a variable that, if given a chance, will show us an error. The value of this variable will never be a chance. The value of this variable will always be the same. And since this variable and ES6 has been coming, this variable is the bulk variable. We can use this variable by dividing the function. If we ever want to use it from outside the function, we have to retent it. However, this variable can never be given a chance.

example:

const name = "Mehedy";
name = "Hassan";
console.log(name);
// output: error name^ = "Hassan"
const number = 2;
console.log(number);
// output: 2

--

--