Javascript Number Reference Type

Introduction

The Number type is the reference type for numeric values.

To create a Number object, use the Number constructor and pass in any number.

let numberObject = new Number(10); 

The Number type overrides valueOf(), toLocaleString(), and toString().

The valueOf() method returns the primitive numeric value represented by the object.

toLocaleString() and toString() methods return the number as a string.

The toString() method optionally accepts a single argument indicating the radix in which to represent the number:

let num = 10;/*from   w  w w . ja  va  2s.  c o m*/
console.log(num.toString()); // "10" 
console.log(num.toString(2)); // "1010" 
console.log(num.toString(8)); // "12" 
console.log(num.toString(10)); // "10" 
console.log(num.toString(16)); // "a" 

The Number type has several additional methods used to format numbers as strings.

The Number object gives important functionality to numeric values but really should not be instantiated directly.

The typeof and instanceof operators work differently when dealing with primitive numbers versus reference numbers:

let numberObject = new Number(10); 
let numberValue = 10; 
console.log(typeof numberObject);       // "object" 
console.log(typeof numberValue);        // "number" 
console.log(numberObject instanceof Number);  // true 
console.log(numberValue instanceof Number);   // false 

Primitive numbers always return "number" when typeof is called on them.

Number objects return "object" on typeof operator.

A Number object is an instance of Number, but a primitive number is not.

The Number JavaScript object is a wrapper object for numerical values.

A Number object is created using the Number() constructor.

A primitive type object number is created using the Number() function.

The JavaScript Number type is double-precision 64-bit binary format IEEE 754 value.

The following example uses the Number object's properties:

const biggestNum     = Number.MAX_VALUE;
console.log(biggestNum);/*from  w  w w  .j ava  2  s .  co  m*/
const smallestNum    = Number.MIN_VALUE;
console.log(smallestNum);
const infiniteNum    = Number.POSITIVE_INFINITY;
console.log(infiniteNum);
const negInfiniteNum = Number.NEGATIVE_INFINITY;
console.log(negInfiniteNum);
const notANum        = Number.NaN;
console.log(notANum);

Integer range for Number

The following example shows the minimum and maximum integer values represented as Number object.

const biggestInt  = Number.MAX_SAFE_INTEGER  //  (253 - 1) =>  9007199254740991
console.log(biggestInt);/*from   www  .j ava 2  s.  co  m*/
const smallestInt = Number.MIN_SAFE_INTEGER  // -(253 - 1) => -9007199254740991
console.log(smallestInt);

Larger numbers can be represented using the BigInt type.

The following example converts the Date object to a numerical value using Number as a function:

let d = new Date('December 17, 2020 03:24:00')
console.log(Number(d))

Convert numeric strings and null to numbers

console.log(Number('123'));     // 123
console.log(Number('12.3'));    // 12.3
console.log(Number('12.00'));   // 12
console.log(Number('123e-1'));  // 12.3
console.log(Number(''));        // 0
console.log(Number(null));      // 0
console.log(Number('0x11'));    // 17
console.log(Number('0b11'));    // 3
console.log(Number('0o11'));    // 9
console.log(Number('test'));     // NaN
console.log(Number('100a'));    // NaN
console.log(Number('-Infinity')); //-Infinity



PreviousNext

Related