Our Javascript Code :-
1. What’s in x?
xis a primitive number with the value123.- You can check:
2. What’s in y?
yis an object, specifically a Number wrapper object, created by thenew Number(…)constructor.- Although it “holds” the numeric value
123, it’s really an object. - You can check:
3. How does === (strict equality) work?
- The strict equality operator
===checks both value and type, without doing any type conversion.
-
If the types differ (e.g.,
"string"vs."number", or"number"vs."object"), returnfalseimmediately. - 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
- Check types
typeof y→"object"typeof x→"number"- Since
"number"≠"object", strict equality short‑circuits and returnsfalseright away. - Result:
5. Why might you see true with ==?
If you used loose equality == instead:
==will convert the objectyto its primitive value (123) viay.valueOf(), then compare123 == 123, which istrue.
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.
