There's 2 data types number in JavaScript: number
and BigInt
. For normal use, number
will be used most of the case. Unless specfed, it will be number
.
JavaScript numbers are stored as double precision floating point numbers.
Numbers are stored in 64 bits:
Integers (numbers without a period or exponent notation) are accurate up to 15 digits. The maximum number of decimals is 17. Check out previous link for floating precision too.
Declaration of variables in JavaScript doesn't need to specify the type. In the case of numbers, just don't put quotation mark that will mark it as a string
.
And even when variable's type is a string (quoted), but if it's consist solely of number, then mathematical operation is possible, given the operation is non ambiguosly a number operation. Keep n mind that addition operation +
is not just a number operation, but also a string operation (concatenation), and concatenation of strings is prioritized.
5 + 3 ; // 8 5 + "3" ; // '53' 5 - 3 ; // 2 5 - "3" ; // 2 "5" - "3" ; // 2
NaN
is a number value of "Not a Number". When mathematical operation is conducted to a non number (eg. string that can't be changed nto a number), the result will be NaN
.
Function isNaN()
can be used to find out if a value is a not a number.
5 + "squid" ; // '5squid' 5 - "squid" ; // NaN 5 + squid ; // error, squid is not a defined variable.
Infinity
(or -Infinity
): the value that will return if you calculate a number outside the largest possible number.
let num = 2; while (num != Infinity) { num = num * num; console.log(num); } ------------ Console log: 4 16 256 65536 4294967296 18446744073709552000 3.402823669209385e+38 1.157920892373162e+77 1.3407807929942597e+154 Infinity
Number()
Converting to number
data types: Number()
Checking data types: typeof myVar
num = "555"; num + 3; // '5553' Number(num) + 3; // 558
Remember if the variable type actually want to be changed, it needs to be put in equation and override the original string value.
num = "555"; typeof num; // 'string' Number(num); typeof num; // 'string' typeof Number(num); // 'number' num = Number(num); typeof num; // 'number'
toFixed()
To round a number to a fixed number of decimal places. Type is string. Use Number()
to further change the type into number.
let numLong = 1.766584958675746364; let numShort = numLong.toFixed(2); numLong; // 1.7665849586757463 numShort; // '1.77' Number(numShort); // 1.77
Base | Name | Sign | Example |
---|---|---|---|
2 | Binary | 0b or 0B | let x= 0b10101 |
8 | Octal | 0 | let x= 020; // 25 |
16 | Hexadecimal | 0x or 0X | let x= 0xFF; |
JavaScript number is displayed as base 10 decimals. To output numbers from base 2 to 36, use toString(base)
. The output type willl be string.
let num = 32; num.toString(2); // 100000 num.toString(8); // 40 num.toString(10); // 32 num.toString(16); // 20 num.toString(32); // 10
Operator | Name |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponentiation |
/ | Division |
% | Modulus/Remainder |
Arithmetic operators perform arithmetic on numbers (literals or variables).
Operands: the numbers being operated in an arithmetic operation. Also called arguments.
An operator is unary if t has a single operand. For example, the unary negation -
that reverses the number sign.
An operator is binary if t has a 2 operands For example, the subtraction operator -
.
let x = 50; x = -x; // - here is an unary operator let x = 50; let y = 20; x = x - y; // - here is a binary operator
The plus +
exists in 2 forms: binary for addition and concatenation, and unary. When the operand is already a number, unary doesn't do anything. But when the operand is not a number, unary +
will change it into a number.
+55; // 55 +'55'; // 55 +''; // 0 +' '; // 0 +true; // 1 +false; // 0 +'hello'; // isNaN +hello; // error, hello is not defined
It's the same with method Number()
.
Number('55'); // 55 Number(''); // 0 Number(' '); // 0 Number(true); // 1 Number(false); // 0 Number('hello'); // NaN Number(hello); // error, hello is not defined
Since the unary is shorter, often it's handier when writting code.
let lions = '50'; let tigers = '30'; let bigcat = lions + tigers; // '5030' let Number(lions) + Number(tigers); // 80 let +lions + +tigers; //80
The division of 2 integers produces a quotien and a remainder. The result of modulo operation is the remainder.
let x = 20 / 7; // 2.857142857142857 let x = 20 % 7; // 6
Exponent x**y
has the same function as Math.pow(x,y)
. It raises x to the power of y. In school math: xy.
Example with base = 7 and exponent = 3:
5**3; // 125 Math.pow(5,3); // 125 5*5*5 // 125 64**(1/2); // 8 (power of 1/2 or square root) 125**(1/3); // 5 (power of 1/3 or cubic root)
200+300 / 2+3 ; // = 200 + 150 + 3 (200+300) / (2+3) ; // = 500 / 5
Precedence | Name | Sign |
---|---|---|
... | ... | ... |
17 | Unary plus Unary negation |
+ - |
16 | Exponentiation | ** |
15 | Multipication Division Remainder |
* / % |
13 | Addition Subtraction |
+ - |
... | ... | ... |
3 | Assignment Other assignments |
= +=, -=, *=, etc |
... | ... | ... |
More: Operator Precedence Table
Operator | Name | Example | Shortcut for |
---|---|---|---|
++ | Increment | num++; ++num; |
num = num + 1; |
-- | Decrement | num--; --num; |
num = num - 1; |
For num++
and num--
, the browser will first return the number, then doing the increment/decrement.
For num++
and num--
, the browser will do the increment/decrement first, then returning the number.
let num = 50; // 50 num++; // 50 num; // 51 let num = 50; // 50 ++num; // 51 num; // 51
We can even isolate this effect.
let counter = 50; let freeze = counter++; freeze; // 50 counter; // 51 let counter = 50; let freeze = ++counter; freeze; // 51 counter; // 51 let counter = 1; alert( 2 * ++counter ); // 4 let counter = 1; alert( 2 * counter++ ); // 2
Operation works the same for numbers in string type. It's considered as number, will not get concatenated.
let num = '50'; // '50' num++; // 50 num; // 51
Increment and decrement can only be appied to variables, not on the number directly
The most plain asignment operator is the equal sign: =
Assignment operators returns a value.
Note: Don't confuse that with ==
and ===
that are comparison operators.
There can be multiple =
in one line. It's called chained assignment and it's evaluated from right to left.
let a, b, c; a = b = c = 2 + 2; a; // 4 b; // 4 c; // 4
It's the same as:
let a, b, c; c = 2 + 2; // 4 b = c; // 4 a = b; // 4
Let's use the second one because it's clearer for everybody to read.
Operator | Name | Example | Shortcut for |
---|---|---|---|
+= | Addition assignment | num += 3; | num = num + 3; |
-= | Subtraction assignment | num -= 3; | num = num - 3; |
*= | Multiplication assignment | num *= 3; | num = num * 3; |
/= | Division assignment | num /= 3; | num = num / 3; |
Be careful with +=
addition operator in numbers in string format. It will concatenate instead just like regular +
addition. Use Number()
to change the type first into number.
let num = '50'; // '50' num += 3; // '503' num += 3; // '5033' let num = '50'; // '50' num = Number(num); // 50 num += 3; // 53 num += 3; // 56 let num = '50'; // '50' num -= 3; // 47 num -= 3; // 44
These various assignment operators have same precedence as the plain =
assignment operator. Which means they are executed nearly the end.
let n = 2; n *= 3 + 5; // 16
Even though multiplication has higher precedence than addition, the multiplication is conducted as assignment. therefore it has the lower precedence.
Operator | Name | Example | Boolean |
---|---|---|---|
== | Equality | 5 == 2+3 5 == 2+4 |
true false |
!= | Non-equality | 5 != 2+4 5 != 2+3 |
true false |
=== | Strict equality | 5 == '5' 5 === '5' |
true false |
!== | Strict non-equality | 5 != '5' 5 !== '5' |
false true |
< | Less than | 5 < 10 5 < 5 |
true false |
> | Greater than | 10 > 5 10 > 10 |
true false |
<= | Less than or equal to | 5 <= 10 5 <= 5 |
true true |
>= | Greater than or equal to | 10 >= 5 10 >= 10 |
true true |
In strict equality ===
and non-equality !==
, data type must be the same too. In formar version regular equality and non-equality, data type can be different. Use strict ones.
Numbers can be defined as an object with new Number()
method. Remember method Number()
is to make a string type number into number type. In general, stick with real number, not object number.
let x = 123; // 123, type: number let y = new Number(123) // Number {123}, type: object let z = new Number(123) // Number {123}, type: object
Some behaviours:
x==y // true x===y // false y==z // false y===z // false
Comparing two JavaScript objects always returns false.
Bitwise operators treat arguments as 32-bit integer numbers and work on the level of their binary representation. Read more...
AND | & |
OR | | |
XOR | ^ |
NOT | ~ |
LEFT SHIFT | << |
RIGHT SHIFT | >> |
ZERO-FILL RIGHT SHIFT | >>> |
Comma operator ,
is very rare and has very low precedence, even lower than =
.
let a = (1 + 2, 3 + 4); a; // 7
1 + 2 is evaluated and the result is thrown away. Then 3 + 4 is evaluated and returned as the result.
let a = 1 + 2, 3 + 4; a; // 3
First the +
will be evaluated, resulting in a = 3, 7
. Then assignment operator, assigns a = 3
, and the rest is ignored. In other word, it's treated like (a = 1 + 2), 3 + 4
.
Example usage:
for (a = 1, b = 3, c = a * b; a < 10; a++) {...}