A number is signed, and can be an integer, an IEEE 754 float, or one of the special values Infinity, -Infinity, and NaN.

Literal Syntax[edit | edit source]

Definition[edit | edit source]

Numbers have several syntaxes, for convenience.

Decimal[edit | edit source]

A decimal number literal consists of a number of digits, with an optional decimal point and decimal part.

42
2.3
17.24

Scientific[edit | edit source]

For very large or very small numbers, it can often be useful to represent them in 'scientific notation', defined as . In Javascript, this is written as men or mEn, where m and n are signed decimal integers.

5e3  // 5000
5e-3 // 0.005

Hexadecimal[edit | edit source]

It's sometimes handy to represent numbers in hexadecimal. In Javascript, hexadecimal numbers are expressed as a string of digits from 0 to F with a prefix of 0x or 0X. The case used for the alphabetical digits is unimportant.

0x5        // 5
0xF        // 15
0xff       // 255
0xDEADBEEF // 3735928559

Operators[edit | edit source]

These operators can be used on numbers.

Arithmetic Operators[edit | edit source]

These operators perform basic arithmetic with two numbers.

Binary + (Addition)[edit | edit source]

Adds its two operands together. Beware: if either operand is a string this is in fact the concatenation operator.

3 + 5   // 8
2 + 1.9 // 3.9

Binary - (Subtraction)[edit | edit source]

Subtracts the right operand from the left operand.

5 - 3   // 5
2 - 1.9 // 0.1
1 - 2   // -1

* (Multiplication)[edit | edit source]

Multiplies two numbers together.

2 * 5   // 10
5 * 1.5 // 7.5

/ (Division)[edit | edit source]

Divides the left operand by the right operand.

10 / 5    // 2
7.5 / 1.5 // 5
5 / 2     // 2.5

% (Remainder)[edit | edit source]

Divides the left operand by the right operand, and returns the remainder. For floats, uses the truncated form: for all floats a and integers b, a % b is equivalent to (a - Math.floor(a)) + Math.floor(a) % b.

5 % 2   // 1
4 % 2   // 0
5.5 % 2 // 1.5

Transformation Operators[edit | edit source]

Unary - (Negation)[edit | edit source]

Returns the negative equivalent of the number (i.e. the number subtracted from zero).

-5       // -5
-10      // -10
-(5 / 2) // -2.5

Unary + (Identity)[edit | edit source]

Returns the number. Mostly useful for coercing values of other types to number.

+42   // 42
+"42" // 42

Comparison Operators[edit | edit source]

Comparison operators take two numbers and return a boolean value. Any comparison containing NaN will always return false. NaN is therefore the only value unequal to itself, which provides an easy way to check for it (however, isNaN() will get the job done, too).

=== (Equals)[edit | edit source]

Compares two values for equality. Values of different types are automatically considered unequal.

2 + 2 === 5     // false
2 + 2 === 4     // true

!== (Not Equals)[edit | edit source]

Compares two values for inequality; x !== y is equivalent to !(x === y).

2 + 2 !== 5 // true

> (Greater Than)[edit | edit source]

Compares two values and returns true if the first is greater than the second.

2 > 1       // true
2 > 1.5     // true
2 > 2       // false
2 > 3       // false

< (Less Than)[edit | edit source]

Less-than. Opposite of greater-than. a < b is equivalent to b > a.

1 < 2       // true

>= (Greater Than Or Equal To)[edit | edit source]

Similar to greater-than, but also returns true if the values are equal: a >= b is equivalent to a > b || a == b.

2 >= 1 // true
2 >= 2 // true
2 >= 3 // false

<= (Less Than Or Equal To)[edit | edit source]

The opposite of >=. a <= b is equivalent to a < b || a == b.

2 <= 1 // false
2 <= 2 // true
2 <= 3 // true

Bitwise Operators[edit | edit source]

These operators treat all numbers as thirty-two-bit binary values, and apply various binary operations to them.

~ (Bitwise Not)[edit | edit source]

Converts each 1 in the number to a 0, and each 0 to a 1. In these examples, the suffixes 'bin' and 'dec' will be used to indicate that the number is binary or decimal, respectively, where confusing.

~9  // -10

~-9 // 8

& (Bitwise AND)[edit | edit source]

Returns a 1 for each position at which both operands are 1.

4 & 6 //   100 bin
      // & 110 bin
      // = 100 bin
      // = 4 dec

| (Bitwise OR)[edit | edit source]

Returns a 1 for each position at which either operand is 1.

4 | 6 //   100 bin
      // | 110 bin
      // = 110 bin
      // = 6 dec

^ (Bitwise XOR)[edit | edit source]

Returns a 1 for each position at which exactly one of the operands is 1.

4 ^ 6 //   100 bin
      // ^ 110 bin
      // = 010 bin
      // = 2 dec

<< (Bitwise Left-Shift)[edit | edit source]

Shifts the left operand the number of bits to the left specified by the right operand, filling the gap with zeroes.

7 << 2 // 28

>> (Bitwise Sign-Preserving Right-Shift)[edit | edit source]

Shifts the left operand the number of bits to the right specified by the right operand, filling the gap with ones if the number is negative, or zeroes otherwise. Reverses a left-shift.

28 >> 2  // 7

-28 >> 2 // -7

>>> (Bitwise Right-Shift)[edit | edit source]

Like >>, but always fills with zeroes.

28 >>> 2  // 7

-28 >>> 2 // 1073741817
Community content is available under CC-BY-SA unless otherwise noted.