new Number() vs Number()

Javascript

Javascript Problem Overview


What is the difference between new Number() and Number()? I get that new Number() creates a Number object and Number() is just a function, but when should I call which, and why?

On a related note, Mozilla says:

> Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task.

> x = Boolean(expression); // preferred > x = new Boolean(expression); // don't use

Why is that? I thought the results were the same?

Javascript Solutions


Solution 1 - Javascript

Boolean(expression) will simply convert the expression into a boolean primitive value, while new Boolean(expression) will create a wrapper object around the converted boolean value.

The difference can be seen with this:

// Note I'm using strict-equals
new Boolean("true") === true; // false
Boolean("true") === true; // true

And also with this (thanks @hobbs):

typeof new Boolean("true"); // "object"
typeof Boolean("true"); // "boolean"

Note: While the wrapper object will get converted to the primitive automatically when necessary (and vice versa), there is only one case I can think of where you would want to use new Boolean, or any of the other wrappers for primitives - if you want to attach properties to a single value. E.g:

var b = new Boolean(true);
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // will work

var b = true;
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // undefined

Solution 2 - Javascript

new Number( x )

creates a new wrapper object. I don't think that there is a valid reason to ever use this.

Number( x )

converts the passed argument into a Number value. You can use this to cast some variable to the Number type. However this gets the same job done:

+x

Generally:

You don't need those:

new Number()
new String()
new Boolean()

You can use those for casting:

Number( value )
String( value )
Boolean( value )

However, there are simpler solutions for casting:

+x // cast to Number
'' + x // cast to String
!!x // cast to Boolean

Solution 3 - Javascript

Always worth consulting the spec; from Section 15.7.1:

> When Number is called as a function rather than as a constructor, it performs a type conversion.

Similarly, using Boolean as a function (15.6.1):

> When Boolean is called as a function rather than as a constructor, it performs a type conversion.

...which means that you consult Section 9.2 ("ToBoolean"):

> The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:
Undefined = false
Null = false
Boolean = The result equals the input argument (no conversion).
Number = The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String = The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object = true

The difference between new Boolean(value) and Boolean(value) is basically that the former returns an object, but the latter returns a primitive per the above. This matters, because objects are truthy:

var b = new Boolean(false);

display(b);            // Displays "false"
if (b) {
  display("true");     // This is the path that gets taken, displaying "true"
}
else {
  display("false");    // This path does NOT get taken
}

Live example ...whereas you almost always want booleans for the purpose of testing them.

Solution 4 - Javascript

case with instanceof

const a = new Number('123'); // a === 123 is false
const b = Number('123'); // b === 123 is true
a instanceof Number; // is true
b instanceof Number; // is false

Solution 5 - Javascript

// Type conversion to primitive value    
const a = Number('42')
a === 42  // true
a.answer = 'You asked the wrong question'
a.answer // undefined

// Object
const b = new Number('42')
b === 42 // false
b.answer = 'Life, the Universe, and Everything'
b.valueOf() === 42 // true
b.answer // 'Life, the Universe, and Everything'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom TuckerView Question on Stackoverflow
Solution 1 - JavascriptDavid TangView Answer on Stackoverflow
Solution 2 - JavascriptŠime VidasView Answer on Stackoverflow
Solution 3 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 4 - JavascriptVladView Answer on Stackoverflow
Solution 5 - JavascriptGenovoView Answer on Stackoverflow