Number
this wiki
A number is signed, and can be an integer, an IEEE 754 float, or one of the special values Infinity
, -Infinity
, and NaN
.
Contents
[show]Literal Syntax
Edit
Definition
Edit
Numbers have several syntaxes, for convenience.
Decimal
Edit
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
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
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
These operators can be used on numbers.
Arithmetic Operators
Edit
These operators perform basic arithmetic with two numbers.
Binary + (Addition)
Edit
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
Subtracts the right operand from the left operand.
5 - 3 // 5 2 - 1.9 // 0.1 1 - 2 // -1
* (Multiplication)
Edit
Multiplies two numbers together.
2 * 5 // 10 5 * 1.5 // 7.5
/ (Division)
Edit
Divides the left operand by the right operand.
10 / 5 // 2 7.5 / 1.5 // 5 5 / 2 // 2.5
% (Remainder)
Edit
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
Unary - (Negation)
Edit
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
Returns the number. Mostly useful for coercing values of other types to number.
+42 // 42 +"42" // 42
Comparison Operators
Edit
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
Compares two values for equality. Values of different types are automatically considered unequal.
2 + 2 === 5 // false 2 + 2 === 4 // true
!== (Not Equals)
Edit
Compares two values for inequality; x !== y
is equivalent to !(x === y)
.
2 + 2 !== 5 // true
> (Greater Than)
Edit
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
Less-than. Opposite of greater-than. a < b
is equivalent to b > a
.
1 < 2 // true
>= (Greater Than Or Equal To)
Edit
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
The opposite of >=
. a <= b
is equivalent to a < b || a == b
.
2 <= 1 // false 2 <= 2 // true 2 <= 3 // true
Bitwise Operators
Edit
These operators treat all numbers as thirty-two-bit binary values, and apply various binary operations to them.
~ (Bitwise Not)
Edit
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
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
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
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
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
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
Like >>
, but always fills with zeroes.
28 >>> 2 // 7 -28 >>> 2 // 1073741817