What is the difference between a number stored in a normal variable:
var foo = 5;
And a number object:
var bar = new Number(5);
What can I use a number object for?
The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.
If you use Number in a place where a type is expected, TypeScript will not complain, because Number is an interface. If you use Number in a place where a value (something that exists in emitted code) is expected, TypeScript will not complain, because Number is also a global constructor.
The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows IEEE standard to represent the floating-point numbers. By the help of Number() constructor, you can create number object in JavaScript.
Can JavaScript object property be a number? Against what many think, JavaScript object keys cannot be Number, Boolean, Null, or Undefined type values. Object keys can only be strings, and even though a developer can use other data types to set an object key, JavaScript automatically converts keys to a string a value.
A Number
object contains some useful methods and properties such as:
Method Description
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
toExponential(x) Converts a number into an exponential notation
toFixed(x) Formats a number with x numbers of digits after the decimal point
toPrecision(x) Formats a number to x length
toString() Converts a Number object to a string
valueOf() Returns the primitive value of a Number object
Property Description
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
constructor Returns the function that created the Number object's prototype
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a "Not-a-Number" value
POSITIVE_INFINITY Represents infinity (returned on overflow)
prototype Allows you to add properties and methods to an object
I think in practice there is no difference between this two. All available methods for number objects is also available for primitive numbers. When you invoke a method on a primitive number, the number temporarily gets converted to an object and then the method executes. See the following examples:
var temp1 = Object(1) // number object
var temp2 = 1 // primitive number
console.log(temp1.toString()) // invoke its own method. result: 1
console.log(temp2.toString()) // temporarily converts to object and invoke number object method. result:1
console.log(Object(1).constructor === Number) //true
console.log((1).constructor === Number) //true
// ^---------- temporarily converts to object
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