In JavaScript, numbers are implemented in
double-precision 64-bit binary format IEEE 754
(i.e., a number between ±2^−1022 and ±2^+1023, or about ±10^−308 to ±10^+308, with a numeric precision of 53 bits). Integer values up to ±2^53 − 1 can be represented exactly.
In addition to being able to represent floating-point numbers, the number type has three symbolic values:
, -Infinity, and
(not-a-number).
See also
JavaScript data types and structures
for context with other primitive types in JavaScript.
You can use four types of number literals: decimal, binary, octal, and hexadecimal.
js
1234567890 42 Decimal literals can start with a zero (0) followed by another decimal digit, but if all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number. This is considered a legacy syntax, and number literals prefixed with 0, whether interpreted as octal or decimal, cause a syntax error in
— so, use the 0o prefix instead.
js
0888 // 888 parsed as decimal 0777 // parsed as octal, 511 in decimal
Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (0b or 0B). If the digits after the 0b are not 0 or 1, the following
is thrown: "Missing binary digits after 0b".
js
0b10000000000000000000000000000000 // 2147483648 0b01111111100000000000000000000000 // 2139095040 0B00000000011111111111111111111111 // 8388607
The standard syntax for octal numbers is to prefix them with 0o. For example:
js
0O755 // 493 0o644 // 420 There's also a legacy syntax for octal numbers — by prefixing the octal number with a zero: 0644 === 420 and "\045" === "%". If the digits after the 0 are outside the range 0 through 7, the number will be interpreted as a decimal number.
js
const n = 0755; // 493 const m = 0644; // 420
forbids this octal syntax.
Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X). If the digits after 0x are outside the range (0123456789ABCDEF), the following
is thrown: "Identifier starts immediately after numeric literal".
js
0xFFFFFFFFFFFFF // 4503599627370495 0xabcdef123456 // 188900967593046 0XA // 10
js
0e-5 // 0 0e+5 // 0 5e1 // 50 175e-2 // 1.75 1e3 // 1000 1e-3 // 0.001 1E3 // 1000
For all literal syntaxes shown above, an underscore (_) can be inserted between digits to improve readability.
js
1_000_000_000_000 1_050.95 0b1010_0001_1000_0101 0o2_2_5_6 0xA0_B0_C0 1_000_000_000_000_000_000_000n See the
reference for more details about number literals.
The built-in
object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:
js
const biggestNum = Number.MAX_VALUE; const smallestNum = Number.MIN_VALUE; const infiniteNum = Number.POSITIVE_INFINITY; const negInfiniteNum = Number.NEGATIVE_INFINITY; const notANum = Number.NaN; You always refer to a property of the predefined Number object as shown above, and not as a property of a Number object you create yourself.
The following table summarizes the Number object's properties.
PropertyDescription
The largest positive representable number (1.7976931348623157e+308)
The smallest positive representable number (5e-324)
Special "not a number" value
Special negative infinite value; returned on overflow
Special positive infinite value; returned on overflow
Difference between 1 and the smallest value greater than 1 that can be represented as a
(2.220446049250313e-16)
Minimum safe integer in JavaScript (−2^53 + 1, or −9007199254740991)
Maximum safe integer in JavaScript (+2^53 − 1, or +9007199254740991)MethodDescription
Parses a string argument and returns a floating point number. Same as the global
function.
Parses a string argument and returns an integer of the specified radix or base. Same as the global
function.
Determines whether the passed value is a finite number.
Determines whether the passed value is an integer.
Determines whether the passed value is
. More robust version of the original global
.
Determines whether the provided value is a number that is a safe integer.The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.
MethodDescription
Returns a string representing the number in exponential notation.
Returns a string representing the number in fixed-point notation.
Returns a string representing the number to a specified precision in fixed-point notation.
The built-in
object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141…), which you would use in an application as
js
Math.PI; Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write
js
Math.sin(1.56); Note that all trigonometric methods of Math take arguments in radians.
The following table summarizes the Math object's methods.
Methods of MathMethodDescription
Absolute value
,
,
Standard trigonometric functions; with the argument in radians.
,
,
,
Inverse trigonometric functions; return values in radians.
,
,
Hyperbolic functions; argument in hyperbolic angle.
,
,
Inverse hyperbolic functions; return values in hyperbolic angle.
,
,
,
,
,
,
Exponential and logarithmic functions.
,
Returns the largest/smallest integer less/greater than or equal to an argument.
,
Returns the minimum or maximum (respectively) value of a comma separated list of numbers as arguments.
Returns a random number between 0 and 1.
,
,
, Rounding and truncation functions.
,
,
Square root, cube root, Square root of the sum of square arguments.
The sign of a number, indicating whether the number is positive, negative or zero.
,
Number of leading zero bits in the 32-bit binary representation.
The result of the C-like 32-bit multiplication of the two arguments. Unlike many other objects, you never create a Math object of your own. You always use the built-in Math object.
One shortcoming of number values is they only have 64 bits. In practice, due to using IEEE 754 encoding, they cannot represent any integer larger than
(which is 253 - 1) accurately. To solve the need of encoding binary data and to interoperate with other languages that offer wide integers like i64 (64-bit integers) and i128 (128-bit integers), JavaScript also offers another data type to represent arbitrarily large integers:
.
A BigInt can be defined as an integer literal suffixed by n:
js
const b1 = 123n; // Can be arbitrarily large. const b2 = -1234567890987654321n; BigInts can also be constructed from number values or string values using the
constructor.
js
const b1 = BigInt(123); // Using a string prevents loss of precision, since long number // literals don't represent what they seem like. const b2 = BigInt("-1234567890987654321"); Conceptually, a BigInt is just an arbitrarily long sequence of bits which encodes an integer. You can safely do any arithmetic operations without losing precision or over-/underflowing.
js
const integer = 12 ** 34; // 4.9222352429520264e+36; only has limited precision const bigint = 12n ** 34n; // 4922235242952026704037113243122008064n Compared to numbers, BigInt values yield higher precision when representing large integers; however, they cannot represent floating-point numbers. For example, division would round to zero:
js
const bigintDiv = 5n / 2n; // 2n, because there's no 2.5 in BigInt Math functions cannot be used on BigInt values; they only work with numbers.
Choosing between BigInt and number depends on your use-case and your input's range. The precision of numbers should be able to accommodate most day-to-day tasks already, and BigInts are most suitable for handling binary data.
Read more about what you can do with BigInt values in the
section, or the
.
JavaScript's
type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values (UTF-16 code units). Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it. You can create strings using string literals or string objects.
You can declare strings in source code using either single or double quotes:
js
'foo' "bar" Within a string literal, most characters can be entered literally. The only exceptions are the backslash (\, which starts an escape sequence), the quote character being used to enclose the string, which terminates the string, and the newline character, which is a syntax error if not preceded by a backslash.
More advanced strings can be created using escape sequences:
Hexadecimal escape sequences
The number after \x is interpreted as a
number.
js
"\xA9" // "©" Unicode escape sequences
The Unicode escape sequences require at least four hexadecimal digits following \u.
js
"\u00A9" // "©" Unicode code point escapes
With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF. With the four-digit Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same result.
See also
or
String.prototype.codePointAt()
.
js
"\u{2F804}" // the same with simple Unicode escapes "\uD87E\uDC04"
You can call methods directly on a string value:
js
console.log("hello".toUpperCase()); // HELLO The following methods are available on
values:
Query: get the character or character code at a particular string index. Methods include
,
,
, and
.
Search: get information about a substring that conforms to a pattern, or test if a particular substring exists. Methods include
,
,
,
,
,
,
, and
Composition: combine strings into a longer string. Methods include
,
,
, and
.
Decomposition: break a string into smaller strings. Methods include
,
,
,
,
,
, and
.
Transformation: return a new string based on the current string's content. Methods include
,
,
,
,
, and
.
When working with strings, there are two other objects that provide important functionality for string manipulation:
and
. They are introduced in
and
respectively.
are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
Template literals are enclosed by backtick (
) characters (`) instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}).
Any new line characters inserted in the source are part of the template literal. Using normal strings, you would have to use the following syntax in order to get multi-line strings:
js
console.log( "string text line 1\n\ string text line 2", ); // "string text line 1 // string text line 2" To get the same effect with multi-line strings, you can now write:
js
console.log(`string text line 1 string text line 2`); // "string text line 1 // string text line 2"
In order to embed expressions within normal strings, you would use the following syntax:
js
const five = 5; const ten = 10; console.log( "Fifteen is " + (five + ten) + " and not " + (2 * five + ten) + ".", ); // "Fifteen is 15 and not 20." Now, with template literals, you are able to make use of the syntactic sugar making substitutions like this more readable:
js
const five = 5; const ten = 10; console.log(`Fifteen is ${five + ten} and not ${2 * five + ten}.`); // "Fifteen is 15 and not 20." For more information, read about
in the
.