2011-09-12

JavaScript distinguishes between values and objects

In Java Integer is an object and int is a primitive type (a plain value), that can be converted to the Integer object using Java's auto-boxing capability. JavaScript is similar: Executing the following in Chrome's Developer Tools JavaScript console, will show you the difference between an object and a number in JavaScript:


typeof 1
"number"
typeof new Number(1)
"object"
1 instanceof Number
false
new Number(1) instanceof Number
true
So you see that var myNum = 1; isn't the same as var myNum = Number(1);.