bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Numbers

Concept visual

JavaScript Numbers

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Number Types

JavaScript has only one type of number. Numbers can be written with or without decimals.

Example

let x = 3.14;    // A number with decimals let y = 3;       // A number without decimals

Try it

Yourself » Extra large or extra small numbers can be written with scientific (exponent) notation:

Example

let x = 123e5;    // 12300000 let y = 123e-5;   // 0.00123

Try it

Yourself »

Formula

JavaScript Numbers are Always 64 - bit Floating Point

Unlike many other programming languages,

Formula

JavaScript does not define different types of numbers, like integers, short, long, floating - point etc.

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

Formula

Value (aka Fraction/Mantissa)

Exponent

Sign

Formula

52 bits (0 - 51)
11 bits (52 - 62)

1 bit (63) Most programming languages have many number types:

Whole numbers (integers):

Formula

byte (8 - bit), short (16 - bit), int (32 - bit), long (64 - bit)

Real numbers (floating-point):

Formula

float (32 - bit), double (64 - bit).
Javascript numbers are always double (64 - bit floating point).

Integer Precision

Integers (numbers without a period or exponent notation) are accurate up to 15 digits.

Example

let x = 999999999999999;   // x will be 999999999999999 let y = 9999999999999999;  // y will be 10000000000000000

The maximum number of decimals is 17.

Floating Precision

Floating point arithmetic is not always 100% accurate:

let x = 0.2 + 0.1;

Try it

Yourself » To solve the problem above, it helps to multiply and divide:

let x = (0.2 * 10 + 0.1 * 10) / 10;

Adding Numbers and Strings

WARNING !!

Formula

JavaScript uses the + operator for both addition and concatenation.

Numbers are added. Strings are concatenated. If you add two numbers, the result will be a number:

Example

let x = 10;
let y = 20;
let z = x + y;

If you add two strings, the result will be a string concatenation:

Example

let x = "10";
let y = "20";
let z = x + y;

If you add a number and a string, the result will be a string concatenation:

Example

let x = 10;
let y = "20";
let z = x + y;

If you add a string and a number, the result will be a string concatenation:

Example

let x = "10";
let y = 20;
let z = x + y;

A common mistake is to expect this result to be 30:

Example

let x = 10;
let y = 20;
let z = "The result is: " + x + y;

A common mistake is to expect this result to be 102030:

Example

let x = 10;
let y = 20;
let z = "30";
let result = x + y + z;

The JavaScript interpreter works from left to right.

Formula

First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.

Numeric Strings

JavaScript strings can have numeric content:

let x = 100;         // x is a number let y = "100";       // y is a string

JavaScript will try to convert strings to numbers in all numeric operations:

This will work:

let x = "100";
let y = "10";
let z = x / y;

This will also work:

let x = "100";
let y = "10";
let z = x * y;

And this will work:

let x = "100";
let y = "10";
let z = x - y;

Previous

JavaScript Strings

Next

JavaScript Dates