var a = 1;
var b = Number(1);
var c = new Number(1);
I was wondering what is the difference between these three statements.
I understand that first and second statements are same, as if(a===b) gives true, but the third one will create a object of type number.
What I want to know is how these methods are different, and any advantages one will give over the other?
A value like
1is a primitive, not an object. JavaScript will generally promote numbers toNumberobjects when necessary. There’s rarely a reason to explicitly construct one, and there’s certainly no particular “advantage”. There’s also no reason for something likeNumber(1), though theNumberconstructor is one of several ways of coercing a value to be a number.