JavaScript Trap: Why a Number Isn’t Always Just a Number, Why 123 === new Number(123) is False in JavaScript – Explained Simply!

JavaScript Trap Why a Number Is not Always Just a Number Why 123 not equal to new Number123 is False in JavaScript Explained Simply

Our Javascript Code :-

let x = 123;
let y = new Number(123); console.log(x === y);


1. What’s in x?

  • x is a primitive number with the value 123.
  • You can check:

typeof x; // "number"


2. What’s in y?

  • y is an object, specifically a Number wrapper object, created by the new Number(…) constructor.
  • Although it “holds” the numeric value 123, it’s really an object.
  • You can check:

typeof y; // "object"
y instanceof Number; // true


3. How does === (strict equality) work?

  • The strict equality operator === checks both value and type, without doing any type conversion.

  1. If the types differ (e.g., "string" vs. "number", or "number" vs. "object"), return false immediately.
  2. If the types are the same, compare the values:

  • For primitives (numbers, strings, booleans), it checks value.
  • For objects, it checks reference identity (i.e., are they the exact same object in memory?)

4. Evaluating x === y 

  1. Check types
    • typeof y"object"
    • typeof x"number"
  2. Since "number""object", strict equality short‑circuits and returns false right away.
  3. Result:

console.log(x === y); // → false


5. Why might you see true with ==?

If you used loose equality == instead:

x == y; // → true

  • == will convert the object y to its primitive value (123) via y.valueOf(), then compare 123 == 123, which is true.


Key Points :-

  • Primitives (let x = 123) and wrapper objects (let y = new Number(123)) are different under the hood.
  • === demands the same type and, for objects, the same reference.
  • Use primitives directly in almost all cases—creating Number objects is rarely needed and can lead to confusing results like this one.

✔ Final Answer:

B) false

This question appeared in the IBM 2nd round of interview, testing the candidate’s knowledge of primitive data type and primitive data type with wrapper object in JavaScript.

Post a Comment

Previous Post Next Post