JavaScript Numbers
JavaScript Numbers
JavaScript Numbers
Donate Now
(https://www.javascripttutorial.net/donation/)
bigint/) that represent big integer numbers whose values are larger than 253 – 1.
To support various types of numbers, JavaScript uses different number literal formats.
Integer numbers
The following shows how to declare a variable that holds a decimal integer:
https://www.javascripttutorial.net/javascript-number/ 1/5
29/07/2022, 07:55 JavaScript Numbers
Octal (base 8)
When you use the octal and hexadecimal numbers in arithmetic operations, JavaScript treats them as
decimal numbers.
Octal numbers
An octal literal number starts with the digit zero (0) followed by a sequence of octal digits (numbers
from 0 through 7). For example:
console.log(num);
Output:
57
If an octal number contains a number that is not in the range from 0 to 7, the JavaScript engine
ignores the 0 and treats the number as a decimal. For example:
console.log(num);
Output:
80
This implicit behavior might cause issues. Therefore, ES6 introduced a new octal literal
(https://www.javascripttutorial.net/es6/octal-and-binary-literals/) that starts with the 0o followed by a sequence
of octal digits (from 0 to 7). For example:
https://www.javascripttutorial.net/javascript-number/ 2/5
29/07/2022, 07:55 JavaScript Numbers
console.log(num);
Output:
57
If you an invalid number after 0o , JavaScript will issue a syntax error like this:
console.log(num);
Output:
^^
Hexadecimal numbers
Hexadecimal numbers start with 0x or 0X followed by any number of hexadecimal digits (0 through 9,
and a through f). For example:
console.log(num);
Output:
26
Floating-point numbers
https://www.javascripttutorial.net/javascript-number/ 3/5
29/07/2022, 07:55 JavaScript Numbers
To define a floating-point literal number, you include a decimal point and at least one number after
that. For example:
When you have a very big number, you can use e-notation. E-notation indicates a number should be
multiplied by 10 raised to a given power. For example:
console.log(amount);
Output:
31400000
The notation 3.14e7 means that take 3.14 and multiply it by 107 .
Likewise, you can use the E-notation to represent a very small number. For example:
console.log(amount);
Output:
0.0000005
Also, JavaScript automatically converts any floating-point number with at least six zeros after the
decimal point into e-notation. For example:
https://www.javascripttutorial.net/javascript-number/ 4/5
29/07/2022, 07:55 JavaScript Numbers
console.log(amount);
Output:
5e-7
Floating-point numbers are accurate up to 17 decimal places. When you perform arithmetic
operations on floating-point numbers, you often get the approximate result. For example:
console.log(amount);
Output:
0.30000000000000004
Big Integers
JavaScript introduced the bigint type starting in ES2022. The bigint type stores whole numbers
whose values are greater than 253 – 1.
A big integer literal has the n character at the end of an integer literal like this:
Summary
JavaScript Number type reprensents both integer and floating-point numbers.
https://www.javascripttutorial.net/javascript-number/ 5/5