Numbers in JavaScript

JavaScript Numbers can be both integers (3) or floats ( 3.14). JavaScript Numbers can perform addition, subtraction, multiplication, division and Modulus. Thus all arithmetic operations can be performed using javascript numbers.

JavaScript Supports all numbers systems, including Binary , Octal, Decimal and Hexadecimal numbers. A number starting with 0 is by-default octal in JavaScript. For example 010 is 8 in javascript.

var and let are used to declare numbers in JavaScript.

For constants, const is also used.

typeof operator can check datatype of numbers.

Define Numbers in JS


 let a=3;          // number; 
 let b=3.6;        // number; 
 let c=2e3;        // exponential number (2000)
 let d=0xa;        // hexadecimal 
 let e=010;        // octal // (not supported in strict mode)
 let f=0o10;       // octal in es6
 let g=0b100;      // binary in es6
 let h=NaN;        // not a number
 let i=Infinity;   // number greater than 1.7976931348623157308

 let k=1_00_000;   // Numeric literal separators 


Number()

Number() is used to create a new numeric value. It can also convert string to number or NaN(Not a number).

Do not use new Number(), it will create object and is not primitive.

Number.MAX_VALUE

Number.MAX_VALUE returns 1.7976931348623157e+308. This is maximum possible value by javascript numbers. After that, it becomes Infinity.

1.7976931348623157e+308


const max=Number.MAX_VALUE;
console.log(max);

Number.MAX_SAFE_INTEGER

Number.MAX_SAFE_INTEGER returns 9007199254740991. This is safest value by javascript numbers to perform calculations.

JavaScript is 53 bit by-default. Number.MAX_SAFE_INTEGER equals to 253-1, which is 9007199254740991.

To use numbers greater then Number.MAX_SAFE_INTEGER, use BigInt.

9007199254740991

9007199254740991


const max=Number.MAX_SAFE_INTEGER;
const n=253-1;

console.log(max);
console.log(n);

Numbers greater than Number.MAX_SAFE_INTEGER

Numbers greater than Number.MAX_SAFE_INTEGER are not exact. All odd numbers will convert to next or previous even numbers. See example.

9007199254740991

9007199254740992

9007199254740992 ±1

9007199254740994

9007199254740996 ±1


const max=Number.MAX_SAFE_INTEGER;
console.log(max);
console.log(max+1);
console.log(max+2);
console.log(max+3);
console.log(max+4);

Binary, Octal, Decimal and Hexadecimal Numbers

Here is a comparison of Binary numbers, Octal numbers, Decimal numbersand Hexadecimal numbers.

Number Binary
(0-1)
(2 bit)
Octal
(0-7)
(8 bit)
Decimal
(0-9)
(10 bit)
Hexadecimal
(0-f)
(16 bit)
0 0 0 0 0
1 1 1 1 1
2 10 2 2 2
8 1000 10 8 8
10 1010 12 10 a
15 1111 17 15 f
16 10000 20 16 10

Numbers Methods

Numbers Methods are used to convert a number to string, exponential, precision and fixed. Here are number methods with example.

JavaScript Number Methods
MethoduseExample
toString()convert number to string.let x=6;
x.toString()="6";
toLocalString()convert number to local string.let x=6;
x.toLocalString()="6";
toExponential()convert decimal to exponential notation.let x=6;
x.toExponential()="6e+0";
toPrecision(1)convert number to Precise .let x=1.23456;
x.toPrecision(1)="1";
toPrecision(2)convert decimal to precision 2 .let x=1.23456;
x.toPrecision(2)="1.2";
toPrecision(3)convert decimal to precision 3 .let x=1.23456;
x.toPrecision(3)="1.23";
toFixed() to convert a number to string with fixed decimal value const pi=3.1416;
pi.toFixed(); // "3"
pi.toFixed(1); // "3.1"
pi.toFixed(2); // "3.14"
pi.toFixed(3); // "3.142"

String to Numbers in javascript

Javascript variables can be strings or numbers. Like var x="5" is string, but var y=5 is number. Both can be used in arithmetic operations. Except addition, all arithmetic operations are possible with x and y.

Functions to convert string to number.


Number Function

Number Function can convert string to numbers. Number Function can convert both floating/decimal and non-floating/integers. But if a string contains string character like alphabets or special character, number function will return NaN.


    let a="100";
    let b="100.5";
    let c="100px";
    let d="abc100";
    
    Number(a)       //100
    Number(b)       //100.5
    Number(c)       //NaN - Not a Number
    Number(d)       //NaN - Not a Number
    

Number Function Example


parseInt Function

parseInt Function can convert string to numbers, but non-floating/integers values only. parseInt can also convert binary, octal and hexadecimal to decimal numbers.


    let a="100";
    let b="100.5";
    let c="100px";
    let d="abc100";
    
    parseInt(a)       //100
    parseInt(b)       //100
    parseInt(c)       //100
    parseInt(d)       //NaN - Not a Number
    

parseInt function is also used to convert binary to decimal, octal to decimal and hexadecimal to decimal numbers. To do this, pass second parameter (radix) in parseInt function as 2 for binary, 8 for octal, and 16 for hexadecimal. Default radix is 10. See example.


    let a=100;
    
    parseInt(a,2)     // 4, binary number
    parseInt(a,8)     // 64, octal number
    parseInt(a,10)    // 100, decimal number
    parseInt(a,16)    // 256, hexadecimal number     
    

parseInt Example


parseFloat Function

parseFloat Function can convert string to numbers, floating/decimals and non-floating/integers both. parseFloat can also convert binary, octal and hexadecimal to decimal numbers.


    let a="100";
    let b="100.5";
    let c="100px";
    let d="100.5px";
    let e="abc100";
    
    parseFloat(a)       //100
    parseFloat(b)       //100.5
    parseFloat(c)       //100
    parseFloat(d)       //100.5
    parseFloat(e)       //NaN - Not a Number
    

parseFloat Example


isNaN, is Not a Number Function

JavaScript isNaN function returns a boolean value. For example, isNaN("2") is false and isNaN("a") is true. Even isNaN(NaN) is also true.

For strict number checking, use Number.isFinite().


    isNaN(NaN)                      // returns true
    isNaN(1)                        // returns false
    isNaN("1")                      // returns false
    isNaN("a")                      // returns true
    isNaN("2a")                     // returns true

isNaN Example


isFinite

isFinite function tells whether a number is finite or not. In JavaScript, number less than 1e308 or 1e+308 are finite numbers. Numbers greater than 1e308 are Infinite, for example, 2e308 and more are infinite numbers in JavaScript.


    isFinite(Infinity)             // false
    isFinite(2e308)                // false
    isFinite(1e308)                // true
    isFinite(1e307)                // true
    isFinite(123)                  // true

isFinite Example


Number.isFinite

Number.isFinite is strict method to check number in javascript. Like, isNaN is good for most of the cases, but it returns false for isNaN(true), which should be true as boolean is not number.

Number.isFinite check both numbers and finite numbers, but is false for booleans and also numbers in string quotes.


    Number.isFinite(1);               // true
    Number.isFinite('1');             // false
    Number.isFinite(true);             // false
    Number.isFinite(null);             // false
            
  1. Never starts a number with 0 prefix. Use strict mode to avoid this.
  2. Floating numbers can have maximum 16 characters after decimal.