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
This occurs due to floating-point precision errors in JavaScript. The expression π£ Reveal
false
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
In this code, π£ Reveal
5
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
This is a well-known bug in JavaScript. The π£ Reveal
"object"
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
These results are due to the type coercion and the way JavaScript handles the addition operator with different types. Sourceπ£ Reveal
""
"[object Object]"
0
"[object Object][object Object]"
5. What will be the output of the following code?
console.log(1 < 2 < 3);
console.log(3 > 2 > 1);
Reveal
The first expression evaluates to π£ Reveal
true
false
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
In JavaScript, arrays are compared by reference, not by value. Since π£ Reveal
false
false
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
The subtraction operator converts the string π£ Reveal
2
"53"
'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
The π£ Reveal
true
false
==
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
The code logs π£ Reveal
1
4
3
2
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.π€