What is the difference between var a and var a = new Number() and later assigning the value of a to 1
When consoled I see one as undefined and one has Number{}. What would be the most notable differences to be known as implementation progresses.
What is the difference between var a and var a = new Number() and later assigning the value of a to 1
When consoled I see one as undefined and one has Number{}. What would be the most notable differences to be known as implementation progresses.
The difference is that you shouldn't use new Number()
.
new Number
creates an object, which will be overridden the moment you assign some other value to it. The other just creates a primitive.
The only reason you'd use Number
is to parse strings to numeric values:
var a = Number("1e20"),
b = Number("1.6");
(Keep in mind there's parseInt
and parseFloat
as well.)
Or to access a couple of constants:
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;
Don't use Number
as a declaration like that. There's really no point in doing so.
If you're only as assigning numeric values to variables, without any kind of parsing, just assign them:
var a = 5,
b = 5234;
Number() allows you to parse strings or other variable types, it is not meant as declaration, since variables don't have types in javascript.