DEV Community

Cover image for πŸ”₯ 10 javaScript questions before you face rejection ❌
Jorjis Hasan
Jorjis Hasan

Posted on

πŸ”₯ 10 javaScript questions before you face rejection ❌

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

5
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

"object"
Enter fullscreen mode Exit fullscreen mode

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({} + {});
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

""
"[object Object]"
0
"[object Object][object Object]"
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

true
false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

"number"
Enter fullscreen mode Exit fullscreen mode

In JavaScript, NaN (Not-a-Number) is considered a number type. Source



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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

false
false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

2
"53"
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

true
false
Enter fullscreen mode Exit fullscreen mode

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);
})();
Enter fullscreen mode Exit fullscreen mode

πŸ’£ Reveal

Reveal

1
4
3
2
Enter fullscreen mode Exit fullscreen mode

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:

10 JavaScript Interview Questions You HAVE TO KNOW

Top comments (3)

Collapse
 
anadudev profile image
Anadu Godwin

Makes me remember my first time with javascript :D Great content

Collapse
 
jorjishasan profile image
Jorjis Hasan

I wonder how many answers you’ve gotten right?

Collapse
 
jorjishasan profile image
Jorjis Hasan

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.πŸ€—