Console Learner — An interactive JavaScript course.


4.7 Native Value Objects

>>>
new Number(1)
1
As mentioned in 1.2. Object Types, JavaScript contains a number of built-in object types (or constructor functions, really).
>>>
new Number(1) == 1
true
It is common to make assumptions about these objects...
>>>
new Number(1) == new Number(1)
false
...and be wrong. In reality, the Number objects are wrappers around a primitive number value. They are not one and the same.
>>>
new Number(1).valueOf()
1
The built-in valueOf() method helps to explain what just happened. It is called to coerce an object to a primitive value type (i.e. boolean, number or string).
>>>
new Number(1) === 1
false
When avoiding type coercion, the first comparison from above now shows that the values are indeed different. The second result from above is explained by normal object comparison.
>>>
(1).constructor
function Number() {
    [native code]
}
But lets not pretend that logic always prevail. Any primitive boolean, number or string value will happily masquerade as an instance of the corresponding wrapper object.
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.