JavaScript has only one type of number.
Numbers can be written with, or without decimals.
JavaScript numbers can be written with, or without decimals:
Extra large or extra small numbers can be written with scientific (exponent) notation:
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) |
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:
To solve the problem above, it helps to multiply and divide:
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
![]() |
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 as base 16 (hex), base 8 (octal), or base 2 (binary).
Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.
Division by 0 (zero) also generates Infinity:
Infinity is a number: typeOf Infinity returns number.
NaN is a JavaScript reserved word indicating that a value is not a number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
However, if the string contains a numeric value , the result will be a number:
You can use the global JavaScript function isNaN() to find out if a value is a number.
Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN:
Or the result might be a concatenation:
NaN is a number, and typeof NaN returns number:
Normally JavaScript numbers are primitive values created from literals: var x = 123
But numbers can also be defined as objects with the keyword new: var y = new Number(123)
![]() |
Don't create Number objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results: |
---|
When using the == equality operator, equal numbers looks equal:
When using the === equality operator, equal numbers are not equal, because the === operator expects equality in both type and value.
Or even worse. Objects cannot be compared:
![]() |
JavaScript objects cannot be compared. |
---|
Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
Property | Description |
---|---|
MAX_VALUE | Returns the largest number possible in JavaScript |
MIN_VALUE | Returns the smallest number possible in JavaScript |
NEGATIVE_INFINITY | Represents negative infinity (returned on overflow) |
NaN | Represents a "Not-a-Number" value |
POSITIVE_INFINITY | Represents infinity (returned on overflow) |
Number properties belongs to the JavaScript's number object wrapper called Number.
These properties can only be accessed as Number.MAX_VALUE.
Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:
![]() |
Number methods are covered in the next chapter |
---|
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:
Method | Description |
---|---|
Number() | Returns a number, converted from its argument. |
parseFloat() | Parses its argument and returns a floating point number |
parseInt() | Parses its argument and returns an integer |
JavaScript number methods are methods that can be used on numbers:
Method | Description |
---|---|
toString() | Returns a number as a string |
toExponential() | Returns a string, with a number rounded and written using exponential notation. |
toFixed() | Returns a string, with a number rounded and written with a specified number of decimals. |
toPrecision() | Returns a string, with a number written with a specified length |
valueOf() | Returns a number as a number |
![]() |
All number methods return a new value. 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):
toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of characters behind the decimal point:
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:
![]() |
toFixed(2) is perfect for working with money. |
---|
toPrecision() returns a string, with a number written with a specified length:
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:
![]() |
Used on Date(), the Number() method returns the number of milliseconds since 1.1.1970. |
---|
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
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:
If the number cannot be converted, NaN (Not a Number) is returned.
valueOf() returns a number as a number.
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. |
---|
The Math object allows you to perform mathematical tasks on numbers.
The Math object allows you to perform mathematical tasks.
The Math object includes several mathematical methods.
One common use of the Math object is to create a random number:
![]() |
Math has no constructor. No methods have to create a Math object first. |
---|
Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
![]() |
Math.random() always returns a number lower than 1. |
---|
Math.round() rounds a number to the nearest integer:
Math.ceil() rounds a number up to the nearest integer:
Math.floor() rounds a number down to the nearest integer:
Math.floor() and Math.random() can be used together to return a random number between 0 and 10:
JavaScript provides 8 mathematical constants that can be accessed with the Math object:
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
asin(x) | Returns the arcsine of x, in radians |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y,x) | Returns the arctangent of the quotient of its arguments |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x,y,z,...,n) | Returns the number with the highest value |
min(x,y,z,...,n) | Returns the number with the lowest value |
pow(x,y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sin(x) | Returns the sine of x (x is in radians) |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)
A JavaScript date can be written as a string:
or as a number:
Dates written as numbers, specifies the number of milliseconds since January 1, 1970, 00:00:00.
In this tutorial we use a script to display dates inside a <p> element with id="demo":
The script above says: assign the value of Date() to the content (innerHTML) of the element with id="demo".
![]() |
You will learn how to display a date, in a more readable format, at the bottom of this page. |
---|
The Date object lets us work with dates.
A date consists of a year, a month, a day, an hour, a minute, a second, and milliseconds.
Date objects are created with the new Date() constructor.
There are 4 ways of initiating a date:
Using new Date(), creates a new date object with the current date and time:
Using new Date(date string), creates a new date object from the specified date and time:
![]() |
Valid date strings (date formats) are described in the next chapter. |
---|
Using new Date(number), creates a new date object as zero time plus the number.
Zero time is 01 January 1970 00:00:00 UTC. The number is specified in milliseconds:
![]() |
JavaScript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC). One day contains 86,400,000 millisecond. |
---|
Using new Date(7 numbers), creates a new date object with the specified date and time:
The 7 numbers specify the year, month, day, hour, minute, second, and millisecond, in that order:
Variants of the example above let us omit any of the last 4 parameters:
![]() |
JavaScript counts months from 0 to 11. January is 0. December is 11. |
---|
When a Date object is created, a number of methods allow you to operate on it.
Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of objects, using either local time or UTC (universal, or GMT) time.
![]() |
Date methods are covered in a later chapter. |
---|
When you display a date object in HTML, it is automatically converted to a string, with the toString() method.
Is the same as:
The toUTCString() method converts a date to a UTC string (a date display standard).
The toDateString() method converts a date to a more readable format:
![]() |
Date objects are static, not dynamic. The computer time is ticking, but date objects, once created, are not. |
---|
When setting a date, without specifying the time zone, JavaScript will use the browser's time zone.
When getting a date, without specifying the time zone, the result is converted to the browser's time zone.
In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to CDT (Central US Daylight Time) if a user browses from central US.
![]() |
Read more about time zones in the next chapters. |
---|