Here are 10 perplexing questions, each accompanied by a code snippet. To enhance your learning experience, answers are concealed within a {% details π£ Reveal %}
tagβclick 'Reveal' to uncover them. Let's dive in! π
Each correct answer will get 10 points. What's your score? π
Comment Down.
1. What will be the output of the following code?
console.log(0.1 + 0.2 === 0.3);
π£ Reveal
Reveal
false
This occurs due to floating-point precision errors in JavaScript. The expression 0.1 + 0.2
results in 0.30000000000000004
, which is not strictly equal to 0.3
. Source
2. What does the following code output?
(function() {
var a = b = 5;
})();
console.log(b);
π£ Reveal
Reveal
5
In this code, b
is assigned to the global scope because var
is only applied to a
. Therefore, b
becomes a global variable, accessible outside the function. Source
3. What will be the output of this code?
console.log(typeof null);
π£ Reveal
Reveal
"object"
This is a well-known bug in JavaScript. The typeof
operator returns "object"
for null
, even though null
is not actually an object. Source
4. What does the following code print?
console.log([] + []);
console.log([] + {});
console.log({} + []);
console.log({} + {});
π£ Reveal
Reveal
""
"[object Object]"
0
"[object Object][object Object]"
These results are due to the type coercion and the way JavaScript handles the addition operator with different types. Source
5. What will be the output of the following code?
console.log(1 < 2 < 3);
console.log(3 > 2 > 1);
π£ Reveal
Reveal
true
false
The first expression evaluates to true
because 1 < 2
is true
, and true < 3
is also true
. The second expression evaluates to false
because 3 > 2
is true
, and true > 1
is false
. Source
6. What does the following code output?
console.log(typeof NaN);
7. What will be the output of this code?
let a = [1, 2, 3];
let b = [1, 2, 3];
console.log(a == b);
console.log(a === b);
π£ Reveal
Reveal
false
false
In JavaScript, arrays are compared by reference, not by value. Since a
and b
are two different objects in memory, both comparisons return false
. Source
8. What does the following code print?
console.log('5' - 3);
console.log('5' + 3);
π£ Reveal
Reveal
2
"53"
The subtraction operator converts the string '5'
to a number, resulting in 2
. The addition operator concatenates the string '5'
and the number 3
, resulting in '53'
. Source
9. What will be the output of the following code?
console.log(0 == false);
console.log(0 === false);
π£ Reveal
Reveal
true
false
The ==
operator performs type coercion, so 0
is considered equal to false
. The ===
operator checks for both value and type, so 0
and false
are not strictly equal. Source
10. What does the following code output?
(function() {
console.log(1);
setTimeout(function(){ console.log(2) }, 1000);
setTimeout(function(){ console.log(3) }, 0);
console.log(4);
})();
π£ Reveal
Reveal
1
4
3
2
The code logs 1
and 4
immediately. The setTimeout
with 0
delay is placed in the event queue and executed after the current stack is cleared, so 3
is logged next. Finally, the setTimeout
with 1000
ms delay logs 2
. Source
These questions delve into JavaScript's quirks and common pitfalls. Understanding them will enhance your problem-solving skills and prepare you for technical interviews. Happy coding! π
For a more in-depth exploration of JavaScript interview questions, check out this comprehensive resource.
For a visual walkthrough of similar JavaScript interview questions, you might find this video helpful:
Top comments (3)
Makes me remember my first time with javascript :D Great content
I wonder how many answers youβve gotten right?
Glad you liked it π. JavaScript can be a bit tricky, but itβs the backbone of the internet. Understanding the confusion is crucial for leading the industry.π€