Note the two different uses of the equals sign: A single equals sign (=) is used to assign a value to a variable. A triple equals sign (===) is used to compare two values (see Equality Operators).
==!==== (preferred)!== (preferred)How does this work in practice?
1 2 3 4 5 6 71 == 1; // -> true 7 == "7"; // -> true 1 != 2; // -> true 5 === 5; // -> true 9 === "9"; // -> false 3 !== 3; // -> false 3 !== "3"; // -> true
why does
7 == '7'returns true and9 === '9'returns false?
We strongly recommend that you always use the strict form when comparing for equality (===) or inequality (!==). Use the non-strict forms only when there is a compelling reason to do so (you will be hard pressed to find such a reason).
>>=<<=1 2 3 44 > 3; // -> true 3 >= 3; // -> true 13 < 12; // -> false 3 <= 4; // -> true
More about comparison operators
+-*/%1 2 3 4 58 + 9; // -> 17, adds two numbers together. 20 - 12; // -> 8, subtracts the right number from the left. 3 * 4; // -> 12, multiplies two numbers together. 10 / 5; // -> 2, divides the left number by the right. 8 % 3; /// -> 2, as three goes into 8 twice, leaving 2 left over.
More about Arithmetic operators
&&||1 2 3 4true && false; //-> false false && true; //-> false false || true; //-> true true || false; //-> true
Given that x = 6 and y = 3
1 2 3x < 10 && y > 1; // -> true x === 5 || y === 5; // -> false x !== y; // -> true
Logical NOT
!1 2true === !false; false === !true;
More about logical operators
To get the type of a value assigned to a variable, use the following code:
1 2 3let bar = 42; typeof bar; //-> 'number' typeof typeof bar; //-> 'string'
So the data type of what typeof returns is always a string, bar on the other hand is still a number.
In addition to the simple assignment operator = there are also compound assignment operators such as +=. The following two assignments are equivalent:
1 2x += 1; x = x + 1;
| Operator | Example | Same As |
|---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
Also check out special characters and their names