Search w3schools.com:

SHARE THIS PAGE

JavaScript Number Object


JavaScript has only one type of number.

Numbers can be written with, or without decimals.


JavaScript Numbers

JavaScript numbers can be written with, or without decimals:

Example

var pi=3.14;    // Written with decimals
var x=34;       // Written without decimals

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

Example

var y=123e5;    // 12300000
var z=123e-5;   // 0.00123


All JavaScript Numbers are 64-bit

JavaScript is not a typed language. Unlike many other programming languages, it does not define different types of numbers, like integers, short, long, floating-point etc.

All numbers in JavaScript are stored as 64-bit (8-bytes) base 10, floating point numbers.


Precision

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

The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

Example

var x=0.2+0.1;

Try it yourself »


Octal and Hexadecimal

JavaScript interprets numeric constants as octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and x.

Example

var y=0377;
var z=0xFF;

lamp Never write a number with a leading zero, unless you want an octal conversion. 


Number Properties and Methods

Properties:

  • MAX VALUE
  • MIN VALUE
  • NEGATIVE INFINITY
  • POSITIVE INFINITY
  • NaN
  • prototype
  • constructor

Methods:

  • toExponential()
  • toFixed()
  • toPrecision()
  • toString()
  • valueOf()

Complete Number Object Reference

For a complete reference of all the properties and methods that can be used with the Number object, go to our Complete Number Object Reference.

The reference contains both descriptions and examples, for each property and method.

 


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]