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?
Boolean(expression)will simply convert the expression into a boolean primitive value, whilenew Boolean(expression)will create a wrapper object around the converted boolean value.The difference can be seen with this:
And also with this (thanks @hobbs):
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: