JavaScript Number Object

## JS Basic

JS HOME JS Introduction JS How To JS Output JS Statements JS Comments JS Variables JS Data Types JS Objects JS Functions JS Operators JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Breaks JS Errors JS Validation

## JS Advanced

JS Object JS Number JS String JS Date JS Array JS Boolean JS Math JS RegExp

## JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Events DOM Navigation DOM Nodes DOM Nodelist

## JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS PopupAlert JS Timing JS Cookies

## JS Libraries

JS Libraries JS jQuery JS Prototype

## JS Examples

JS Basic Examples JS Objects Examples JS DOM Examples JS HTML Examples JS Browser Examples JS Quiz JS Certificate JS Summary

## JS References

Javascript Objects HTML DOM Objects

# JavaScript Number Object

« Previous
Next Chapter »

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; // A number written with decimals
var x=34; // A number 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

## JavaScript Numbers are 64-bit Floating Point

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.

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)

## 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; // result will be 0.30000000000000004
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;
Try it yourself »

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

By default, Javascript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).

## Example

var myNumber=128;
myNumber.toString(16); // returns 80
myNumber.toString(8); // returns 200
myNumber.toString(2); // returns 10000000
Try it yourself »

## Infinity

If you calculate a number outside the largest number provided by Javascript, Javascript will return the value of Infinity or -Infinity (positive or negative overflow):

## Example

myNumber=2;
while (myNumber!=Infinity)
{
myNumber=myNumber*myNumber; // Calculate until Infinity
}
Try it yourself »

Division by 0 (zero) also generates Infinity:

## Example

var x = 2/0;
var y = -2/0;
Try it yourself »

| Note: Infinity is a number (typeof(Infinity) returns a number).

## NaN - Not a Number

NaN is JavaScript reserved word indicating that the result of a numeric operation was not a number.

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

## Example

var x = 1000 / "Apple";
isNaN(x); // returns true
var y = 100 / "1000";
isNaN(y); // returns false
Try it yourself »

Division by 0 (zero) generates Infinity, but Infinity is a number:

## Example

var x = 1000 / 0;
isNaN(x); // returns false
Try it yourself »

## Numbers Can be Numbers or Objects

JavaScript numbers can be primitive values created from literals, like var x = 123;

JavaScript number can also be objects created with the new keyword, like var y = new Number(123);

## Example

var x = 123;
var y = new Number(123);
typeof(x) // returns Number
typeof(y) // returns Object
Try it yourself »

Normally, because of some nasty side effects, you will not define numbers as objects.

## Example

var x = 123;
var y = new Number(123);
(x === y) // is false because x is a number and y is an object.
Try it yourself »

## Number Properties

- MAX_VALUE
- MIN_VALUE
- NEGATIVE_INFINITY
- POSITIVE_INFINITY
- NaN
- prototype
- constructor

All number properties are properties of JavaScripts' number object wrapper called Number.

These properties can only be accesses as Number.MAX_VALUE.

Using num.MAX_VALUE, where num is a created object or a primitive number value, will return undefined.

## Number Methods

- toExponential()
- toFixed()
- toPrecision()
- toString()
- valueOf()

Note: Primitive values, like 3.14, cannot have properties and methods (because they are not objects).

With JavaScript, all methods of the number object are also available to primitive values, because Javascript will temporarily transfer primitive values to objects before executing the methods.

## 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.

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE