If You Can Answer These 7 Questions Correctly You’re Decent at JavaScript

Safdar Ali - Sep 27 - - Dev Community

JavaScript can be both fascinating and frustrating. Even seasoned developers can get tripped up by its quirky behaviors. Want to know if you're really decent at JavaScript? Answer these seven questions to find out!

Each of these problems might seem simple at first glance, but they test various aspects of JavaScript, from type coercion to scope and closures. If you can answer them correctly, congratulations — you’ve got a solid grasp of JavaScript!

Question 1: What’s the Result of 0.1 + 0.2 === 0.3?

console.log(0.1 + 0.2 === 0.3);
Enter fullscreen mode Exit fullscreen mode

Answer: The result is false.

Explanation: In JavaScript, numbers with decimals (called floating-point numbers) are stored as approximations, not exact values. When you add 0.1 + 0.2, you don’t get exactly 0.3, but rather 0.30000000000000004. Because of this precision issue, 0.1 + 0.2 is not strictly equal to 0.3.

Question 2: What’s the Result of "5" + 3 and "5" - 3?

console.log("5" + 3);
console.log("5" - 3);
Enter fullscreen mode Exit fullscreen mode

Answer:

  • "5" + 3 results in "53".
  • "5" - 3 results in 2.

Explanation: In the first example, + triggers string concatenation. Since "5" is a string, JavaScript converts the number 3 into a string and combines the two to form "53". In the second case, - tries to perform a mathematical operation, so "5" is coerced into the number 5, and the result is 2.

Question 3: What’s the Value of null == undefined?

console.log(null == undefined);
Enter fullscreen mode Exit fullscreen mode

Answer: The result is true.

Explanation: In JavaScript, null and undefined are considered loosely equal when using the == operator because both represent "absence of value." However, if you use the strict equality operator ===, the result would be false, as they are of different types (null is an object, undefined is a primitive).

Question 4: What’s the Output of the Following Function?

(function(){
  var x = 1;
  if (x === 1) {
    var x = 2;
    console.log(x); // ?
  }
  console.log(x); // ?
})();
Enter fullscreen mode Exit fullscreen mode

Answer:

  • First console.log(x) outputs 2.
  • Second console.log(x) also outputs 2.

Explanation: The var keyword in JavaScript is function-scoped, not block-scoped. This means that the var x = 2 inside the if block overwrites the x declared outside of the block. Therefore, both console.log statements print 2.

If let or const had been used instead of var, the output would be different as they are block-scoped.

Question 5: What’s the Output of typeof NaN?


console.log(typeof NaN);
Enter fullscreen mode Exit fullscreen mode

Answer: The result is "number".

Explanation: Despite its name, NaN (which stands for "Not-a-Number") is still considered a number in JavaScript. The typeof operator will return "number" when called on NaN. This is because NaN is technically a numeric value — it's just a special one that represents an invalid or unrepresentable number.

Question 6: What’s the Difference Between let, var, and const?

Answer:

  • var: Function-scoped and can be re-declared and updated. It is hoisted, meaning it’s initialized with undefined during the compile phase but assigned a value during runtime.
  • let: Block-scoped and can be updated but not re-declared within the same scope. It is not initialized during hoisting, so accessing it before its declaration results in a ReferenceError.
  • const: Block-scoped and cannot be updated or re-declared. The value must be assigned when it’s declared. Though the reference itself is constant, objects or arrays declared with const can still have their properties or elements modified.

Explanation: These keywords are crucial for controlling the scope and mutability of variables in JavaScript. Proper use of them helps to prevent unexpected bugs and improves code clarity.

Question 7: What’s the Output of This Closure Example?

function outer() {
  var x = 10;
  return function inner() {
    console.log(x); // ?
  };
}
var closure = outer();
closure();
Enter fullscreen mode Exit fullscreen mode

Answer: The result is 10.

Explanation: Closures in JavaScript allow functions to access variables from an outer function even after that outer function has executed. In this example, the inner function retains access to the x variable declared in outer, which has a value of 10. Therefore, closure() logs 10.

Bonus: Question 8 – What’s the Output of [] + [] and [] + {}?

console.log([] + []);
console.log([] + {});
Enter fullscreen mode Exit fullscreen mode

Answer:

  • [] + [] results in an empty string "".
  • [] + {} results in "[object Object]".

Explanation: The + operator tries to coerce both operands into strings. For two arrays, JavaScript converts them both to empty strings, resulting in "". For the second example, the array becomes an empty string, and the object becomes its string representation: "[object Object]".

Final Thoughts

If you could answer all of these questions correctly, you’re doing great! These problems reveal the nuances of JavaScript that can trip up even experienced developers. JavaScript's type coercion, scoping, and quirks with numbers all make it an exciting, if sometimes frustrating, language to master.

If you struggled with any of these questions, don’t worry! Keep practicing, and you’ll soon gain a deeper understanding of how JavaScript works behind the scenes.

That's all for today.

And also, share your favourite web dev resources to help the beginners here!

Connect with me:@ LinkedIn and checkout my Portfolio.

Explore my YouTube Channel! If you find it useful.

Please give my GitHub Projects a star ⭐️

Thanks for 31576! 🤗

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .