Skip to main content

Command Palette

Search for a command to run...

JavaScript Quirks That Will Make You Say “Wait, What?”

Strange-looking JavaScript behaviors explained with simple examples and the rules behind them.

Updated
7 min readView as Markdown
JavaScript Quirks That Will Make You Say “Wait, What?”
M
Software Developer

Question - 1

console.log(null === undefined) //false

Why it's false.👀

  • Expression*:* null === undefined

  • Result*:* false

Explanation

In JavaScript, both null and undefined represent "empty" values but are distinct (different) types.

null is a special object representing the intentional absence of a value, while undefined signifies that a variable has been declared but not assigned a value.

Despite their similar purpose, they are not strictly equal (===) to each other.

  • null === undefined evaluates to false because JavaScript does not perform type coercion with ===.

Question - 2

console.log(5 > 3 > 2) //false

Why it's false.👀

  • Expression*:* 5 > 3 > 2

  • Result*:* false

Explanation

At first glance, this expression may appear to be checking if 5 is greater than 3 and 3 is greater than 2, but JavaScript evaluates it left-to-right due to its operator precedence.

  • First, 5 > 3 evaluates to true.

  • Then, true > 2 is evaluated, which in JavaScript results in 1 > 2 (since true is coerced to 1), which evaluates to false.

So, 5 > 3 > 2 evaluates to false.

Question - 3

console.log([] === []) //false

Why it's false.👀

  • Expression*:* [] === []

  • Result*:* false

Explanation

In JavaScript, arrays are objects. Even if two arrays have the same content, they are still different objects in memory.

  • When you compare two arrays with ===, you are comparing their references, not their contents.

  • Since [] and [] are different instances in memory, so the result is false.

Question - 4

console.log("10" < "9"); //true

Why it's true.👀

  • Expression*:* "10" < "9"

  • Result*:* true

Explanation

When JavaScript compares strings, it compares their Unicode values lexicographically (character by character).

  • "10" is compared to "9". Since "1" has a lower Unicode value than "9", JavaScript determines that "10" is less than "9".

  • This comparison might seem counterintuitive, but it's due to JavaScript's string comparison mechanism.

Question - 5

console.log(NaN === NaN);

Why it's false.👀

  • Expression*:* NaN === NaN

  • Result*:* false

Explanation

In JavaScript, NaN (Not-a-Number)is a special value that represents an invalid number or the result of an operation that cannot produce a valid number.

  • One of the most unusual aspects of NaN is that it is not equal to itself. This behavior exists due to the design of the IEEE 754 standard, which JavaScript follows for floating-point arithmetic.

  • As a result, NaN === NaN returns false.

To check if a value is