Why Is New Number(8) Not Exactly Equal To 8?
Solution 1:
The primitive types Boolean, Number and String, each have a corresponding object representation, which can be created via new Boolean
, new String
, etc. As I already hinted at, those return objects. An Object is a different data type than a Number, so strict comparison will return false
.
However, those constructors are not widely used, because, as you found out, they don't play well with primitives. A Number
object that encapsulates the same value as a primitive number value is not (strictly) equal to said primitive value.
What you might see more often is the use of the Number
function without new
. If called without new
, Number
simply performs type conversion, to a primitive number value.
So why do we have Number
, String
and Boolean
objects at all?
It turns out you are using such objects all the time without (probably) knowing, e.g. when you do
"primitive".substring(0, 5)
In JavaScript, only objects can have properties. Primitive values cannot have properties. And yet you can call the substring
method as if it was a property of the value. That's because JavaScript does something called auto-boxing. When you are trying to use a primitive values like an object (e.g. by accessing a property), JavaScript internally converts the primitive temporarily to its equivalent object version.
Solution 2:
That is because when instantiating using new
the type is object
even if that object's name is Number.
typeof y === "object"
Solution 3:
Y is an object not a number. The new keyword references to objects. So y is a number object with the value 8.
Try alert(typeof y);
Solution 4:
First off, alert does always return undefined
. But it prints stuff on screen. console.log
gives you more detailed and colorful info though.
This problem has two parts. First one is that numbers are normally not Number
instance. The second is that two objects (two instances) of any kind are never exactly equal:
{}=={} // false
Not Number
problem
Now to the Number
issue. Although all numbers in JavaScript are technically a Number
, javascript does not treat them like that and they are not number instance. This is something I don't like very much, but it's what it is. If you create number by instance, it behaves as non-object value:
5 instanceof Number //false
typeof 5 // "number"
Creating number with Number
constructor creates an object, that acts as a Number
:
new Number(5) instanceof Number //true
typeof new Number(5) // "object"
It should be noted that this object is actually not very special. You can make your own number class:
function MyNumber(val) { this.number = 1*val||0; } MyNumber.prototype.valueOf = function() { return this.number; }
This will work just as Number
object does. Of course, just as Number
object, this is lost when you do a math operation:
typeof (MyNumber(6)+MyNumber(4)) // "number"
Two object instances are never exactly equal
This is actually useful feature. But it betrays you here:
new Number(5)===new Number(5) //false
Number
instance will never be exactly equal to anything but itself.
Solution 5:
when you make var y=new Number(8);
it becomes a object not a number and because of that ===
fails to compare both as same.
var x=8;
var y=new Number(8);
alert(typeof x);//number
alert(typeof y);//object
alert(y===x);//false
Post a Comment for "Why Is New Number(8) Not Exactly Equal To 8?"