JavaScript Interview Questions.
Questions you might encounter in your next interview. and I hope this would be helpful to you.
Question 1
What will be the output of the below code snippet?
console.log(1 > 2 > 3); //Output: false
Reason↓
Let break the code into simpler version.
console.log(1 > 2) //Output: false
console.log(false > 3) //So over here,false is considered/valued as 0 and true as 1.
console.log(0 > 3) //0 is less than 3.so the result is false.
Question 2
What will be the output of the below code snippet?
console.log(3 > 2 > 1); //Output: false
Reason↓
Let break the code into simpler version.
console.log(3> 2) //Output: true
console.log(true> 3) //So over here,true is replaced as 1.
console.log(1 > 3) //1 is not greater than 3.so the result is false.
Question 3
What will be the output of the below code snippet?
(function () {
console.log("hello world");
})(); //Output: hello world
Reason↓
The above code snippet is an example of Immediately Invoked Function Expression(IIFE).
An IIFE is a function defined as an expression that runs as soon as it is defined.
Question 4
What will be the output of the below code snippet?
console.log(0.1 +0.2 ===0.3 ); //Output: false
Reason↓
console.log(0.1 +0.2) //Output: 0.30000000000000004
It is because JavaScript's number type uses binary floating-point numbers, btw Click here for more info.
Question 5
What will be the output of the below code snippet?
console.log( typeof null); //Output: object
console.log( typeof NaN); //Output: number