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?
Values of other types can be converted to numbers using the Number() function. The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. This means it can represent fractional values, but there are some limits to what it can store.
numbers = [1,2,3,4,5]; function doubling(number) { number *= 2; return number; } obj = {}; for (var i = 0; i < numbers. length; i++) doubled = doubling(numbers[i]); obj[numbers[i]] = doubled; console. log(obj);
To check if a number is between two numbers: Use the && (and) operator to chain two conditions. In the first condition check that the number is greater than the lower range and in the second, that the number is lower than the higher range. If both conditions are met, the number is in the range.
In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With