0% found this document useful (0 votes)
6 views17 pages

JavaScript Numbers

The document provides an overview of JavaScript numbers, explaining that JavaScript uses a single type of number stored as 64-bit floating point according to the IEEE 754 standard. It discusses number precision, the use of scientific notation, and how JavaScript handles arithmetic with numbers and strings, including the concept of NaN (Not a Number) and Infinity. Additionally, it highlights the differences between primitive numbers and Number objects in JavaScript.

Uploaded by

tester
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

JavaScript Numbers

The document provides an overview of JavaScript numbers, explaining that JavaScript uses a single type of number stored as 64-bit floating point according to the IEEE 754 standard. It discusses number precision, the use of scientific notation, and how JavaScript handles arithmetic with numbers and strings, including the concept of NaN (Not a Number) and Infinity. Additionally, it highlights the differences between primitive numbers and Number objects in JavaScript.

Uploaded by

tester
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/11/26, 11:15 PM JavaScript Numbers

 Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

JavaScript Numbers
❮ Previous Next ❯

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

[Link] 1/17
4/11/26, 11:15 PM JavaScript Numbers

Try it Yourself »
Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

JavaScript Numbers are Always 64-bit Floating


Point
Unlike many other programming languages, 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:

Value (aka Fraction/Mantissa) Exponent Sign

52 bits (0 - 51) 11 bits (52 - 62) 1 bit (63)

Most programming languages have many number types:

Whole numbers (integers):


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

Real numbers (floating-point):


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.

[Link] 2/17
4/11/26, 11:15 PM JavaScript Numbers

 Tutorials 
Example References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let x = 999999999999999; // x will be 999999999999999
let y = 9999999999999999; // y will be 10000000000000000

Try it Yourself »

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;

Try it Yourself »

REMOVE ADS

Adding Numbers and Strings

[Link] 3/17
4/11/26, 11:15 PM JavaScript Numbers
WARNING !!
 Tutorials  References  Exercises  Get Certified Sign In
JavaScript uses the + operator for both addition and concatenation.
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
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;

Try it Yourself »

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

Example

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

Try it Yourself »

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;

[Link] 4/17
4/11/26, 11:15 PM JavaScript Numbers

Try it Yourself »
Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
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;

Try it Yourself »

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;

Try it Yourself »

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;

[Link] 5/17
4/11/26, 11:15 PM JavaScript Numbers

Try it Yourself »
Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

The JavaScript interpreter works from left to right.

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;

Try it Yourself »

This will also work:

[Link] 6/17
4/11/26, 11:15 PM JavaScript Numbers

let xTutorials
= "100";
 References  Exercises  Get Certified Sign In

 let yCSS
HTML = "10";
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let z = x * y;

Try it Yourself »

And this will work:

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

Try it Yourself »

But this will not work:

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

Try it Yourself »

In the last example JavaScript uses the + operator to concatenate the strings.

NaN - Not a Number


NaN is a JavaScript reserved word indicating that a number is not a legal number.

[Link] 7/17
4/11/26, 11:15 PM JavaScript Numbers
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
 Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example

let x = 100 / "Apple";

Try it Yourself »

However, if the string is numeric, the result will be a number:

Example

let x = 100 / "10";

Try it Yourself »

You can use the global JavaScript function isNaN() to find out if a value is a not a number:

Example

let x = 100 / "Apple";


isNaN(x);

Try it Yourself »

Watch out for NaN . If you use NaN in a mathematical operation, the result will also be NaN :

Example

let x = NaN;
let y = 5;
[Link] 8/17
4/11/26, 11:15 PM JavaScript Numbers
let z = x + y;
 Tutorials  References  Exercises  Get Certified Sign In

 Try itCSS
HTML YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO [Link] C

Or the result might be a concatenation like NaN5:

Example

let x = NaN;
let y = "5";
let z = x + y;

Try it Yourself »

NaN is a number: typeof NaN returns number :

Example

typeof NaN;

Try it Yourself »

Infinity
Infinity (or -Infinity ) is the value JavaScript will return if you calculate a number
outside the largest possible number.

Example

let myNumber = 2;
// Execute until Infinity
[Link] 9/17
4/11/26, 11:15 PM JavaScript Numbers
while (myNumber != Infinity) {
 Tutorials= myNumber
myNumber References  Exercises 
* myNumber; Get Certified Sign In
}
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Try it Yourself »

Division by 0 (zero) also generates Infinity :

Example

let x = 2 / 0;
let y = -2 / 0;

Try it Yourself »

Infinity is a number: typeof Infinity returns number .

Example

typeof Infinity;

Try it Yourself »

Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

Example

let x = 0xFF;

[Link] 10/17
4/11/26, 11:15 PM JavaScript Numbers

Try it Yourself »
Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Never write a number with a leading zero (like 07).


Some JavaScript versions interpret numbers as octal if they are written with a leading zero.

By default, JavaScript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers from base 2 to base 36.

Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.

Example

let myNumber = 32;


[Link](32);
[Link](16);
[Link](12);
[Link](10);
[Link](8);
[Link](2);

Try it Yourself »

JavaScript Numbers as Objects


Normally JavaScript numbers are primitive values created from literals:

let x = 123;

But numbers can also be defined as objects with the keyword new :
[Link] 11/17
4/11/26, 11:15 PM JavaScript Numbers

let yTutorials  References 


= new Number(123);
Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Example

let x = 123;
let y = new Number(123);

Try it Yourself »

Do not create Number objects.

The new keyword complicates the code and slows down execution speed.

Number Objects can produce unexpected results:

When using the == operator, x and y are equal:

let x = 500;
let y = new Number(500);

Try it Yourself »

When using the === operator, x and y are not equal.

let x = 500;
let y = new Number(500);

Try it Yourself »

[Link] 12/17
4/11/26, 11:15 PM JavaScript Numbers

 Tutorials  References  Exercises 


Note the difference between (x==y) and (x===y) .
Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

(x == y) true or false?

let x = new Number(500);


let y = new Number(500);

Try it Yourself »

(x === y) true or false?

let x = new Number(500);


let y = new Number(500);

Try it Yourself »

Comparing two JavaScript objects always returns false.

Learn More:
JavaScript Number Methods

JavaScript Number Properties

JavaScript Number Reference

[Link] 13/17
4/11/26, 11:15 PM JavaScript Numbers

 Tutorials  References  Exercises 


?
Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL
Exercise
PYTHON JAVA PHP HOW TO [Link] C

Consider the following code:


let x = 10;
let y = 20;
let z = x + y;
What will be the result of z?

30

1020

30.00

Submit Answer »

❮ Previous Sign in to track progress Next ❯

[Link] 14/17
4/11/26, 11:15 PM JavaScript Numbers

 Tutorials  References  Exercises  Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

COLOR PICKER

 
REMOVE ADS

-->
 PLUS SPACES

GET CERTIFIED FOR TEACHERS

[Link] 15/17
4/11/26, 11:15 PM JavaScript Numbers

 Tutorials 
BOOTCAMPS
References  Exercises 
CONTACT US
Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full

[Link] 16/17
4/11/26, 11:15 PM JavaScript Numbers
correctness

 of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and
Tutorials  References  Exercises 
privacy policy.
Get Certified Sign In

HTML
 CSS Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

[Link] 17/17

You might also like