var pi=3.14; // Número con decimales
var x=34; // Número sin decimales
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
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)
52 bits (0 - 51)
Exponent
11 bits (52 - 62)
Sign
1 bit (63)
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:
var x = 0.2+0.1; // result will be 0.30000000000000004
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".
var y = 0377; // Codigo octal
var z = 0xFF; // Codigo hexadecimal
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).
var myNumber=128;
myNumber.toString(16); // returns 80
myNumber.toString(8); // returns 200
myNumber.toString(2); // returns 10000000
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):
myNumber=2;
while (myNumber!=Infinity)
{
myNumber=myNumber*myNumber; // Calculate until Infinity
}
Division by 0 (zero) also generates Infinity:
var x = 2/0;
var y = -2/0;
Note: Infinity is a number (typeof(Infinity) returns 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.
var x = 1000 / "Apple";
isNaN(x); // returns true
var y = 100 / "1000";
isNaN(y); // returns false
Division by 0 (zero) generates Infinity, but Infinity is a number:
var x = 1000 / 0;
isNaN(x); // returns false
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);
var x = 123;
var y = new Number(123);
typeof(x) // returns Number
typeof(y) // returns Object
Normally, because of some nasty side effects, you will not define numbers as objects.
var x = 123;
var y = new Number(123);
(x === y) // is false because x is a number and y is an object.
All number properties are properties of JavaScripts' number object wrapper called Number.
These properties can only be accessed as Number.MAX_VALUE.
Using num.MAX_VALUE, where num is a created object or a primitive number value, will return undefined.
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.
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.
Number methods help you to work with numbers.
JavaScript global functions can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers:
JavaScript number methods are methods that can be used on numbers:
All number methods return a new variable. They do not change the original variable.
toString() returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
var x = 123;
x.toString(); // returns 123 from variable x
(123).toString(); // returns 123 from literal 123
(100 + 23).toString(); // returns 123 from expression 100 + 23
toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of character behind the decimal point:
var x = 9.656;
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns 9.6560e+0
x.toExponential(6); // returns 9.656000e+0
The parameter is optional. If you don't specify it, JavaScript will not round the number.
toFixed() returns a string, with the number written with a specified number of decimals:
var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
toFixed(2) is perfect for working with money.
toPrecision() returns a string, with a number written with a specified length:
var x = 9.656;
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
There are 3 JavaScript functions that can be used to convert variables to numbers:
These methods are not number methods, but global JavaScript methods.
Number(), can be used to convert JavaScript variables to numbers:
x = true;
Number(x); // returns 1
x = false;
Number(x); // returns 0
x = new Date();
Number(x); // returns 1404568027739
x = "10"
Number(x); // returns 10
x = "10 20"
Number(x); // returns NaN
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
parseInt("10"); // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
If the number cannot be converted, NaN (Not a Number) is returned.
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:
parseFloat("10"); // returns 10
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN
If the number cannot be converted, NaN (Not a Number) is returned.
valueOf() returns a number as a number.
var x = 123;
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in your code.
In JavaScript, all data types have a valueOf() and a toString() method.
For a complete reference, go to our Complete JavaScript Number Reference.
The reference contains descriptions and examples of all Number properties and methods.