Operators are the symbols that make things happen: they do the math, store the values, compare things, and combine answers. You have already used a few. This lesson is the map of the whole territory: what each group of operators is for, with a quick taste of each. The next few lessons then go deep on the groups one at a time.
= assigns values+ adds values* multiplies values> compares valueslet x = 5; // assign 5 to x
let y = 2; // assign 2 to y
let z = x + y; // add them, assign the result to z
let big = z > 5; // compare: is z greater than 5?
console.log(z, big);
The values an operator works on are called operands. The operator is the symbol that says what to do with them:
| Operand | Operator | Operand |
|---|---|---|
| 100 | + | 50 |
Most operators take two operands, one on each side. These are called binary operators. A few take just one and are called unary. And exactly one operator takes three, the conditional ? :, which is why it is often just called the ternary (from the Latin for "three"):
// Binary: two operands
5 + 3
10 * 2
// Unary: one operand
-x // negation: flips the sign
!true // logical NOT: gives false
typeof 42 // gives "number"
// Ternary: three operands
age >= 18 ? "adult" : "minor"
| Group | Symbols | What they do | Full lesson |
|---|---|---|---|
| Arithmetic | + - * / % ** ++ -- | Math on numbers | JS Arithmetic |
| Assignment | = += -= *= /= %= **= | Store values in variables | JS Assignment |
| String | + += | Join text together | this lesson |
| Comparison | == === != !== > < >= <= | Compare values, give true/false | JS Comparisons |
| Logical | && || ! | Combine true/false answers | JS Logical |
| Conditional | ? : | Pick one of two values | JS Ternary |
| Type | typeof | Report a value's type | JS typeof |
Arithmetic operators do math on numbers. Operands can be literals, variables, or whole expressions:
let a = 3;
let x = (100 + 50) * a;
console.log(x);
| + | Addition | / | Division |
| - | Subtraction | % | Remainder (modulus) |
| * | Multiplication | ++ | Increment (add 1) |
| ** | Exponentiation (power) | -- | Decrement (subtract 1) |
= stores a value. The compound assignment operators do a bit of math and store the result in one step, so you do not have to write the variable name twice:
let x = 10;
x += 5; // same as x = x + 5
console.log(x);
| 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 |
| **= | x **= y | x = x ** y |
When used on strings, + is called the concatenation operator: it joins them end to end.
let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2; // note the " " for the space
console.log(text3);
+= works on strings too, adding text onto the end of what is already there:
let text = "What a very ";
text += "nice day";
console.log(text);
Adding two numbers gives a number. But if either operand is a string, + joins instead of adding, and the result is a string:
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
console.log(x);
console.log(y);
console.log(z);
+ is the only arithmetic operator that joins strings. The others (- * / %) have no string meaning, so they convert the string to a number and do the math. "10" + 5 is "105", but "10" - 5 is 5. Same values, completely different kinds of answer.
Comparison operators compare two values and always give back true or false. Given let x = 5;:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | equal to | x == 5 | true |
| === | equal value and equal type | x === "5" | false |
| != | not equal | x != 8 | true |
| !== | not equal value or not equal type | x !== "5" | true |
| > | greater than | x > 8 | false |
| < | less than | x < 8 | true |
| >= | greater than or equal to | x >= 8 | false |
| <= | less than or equal to | x <= 8 | true |
They work on strings too, comparing alphabetically: "A" < "B" is true. The JS Comparisons lesson covers the surprises that come with that.
Logical operators combine true/false answers into one:
| && | logical AND: true only if both sides are true |
| || | logical OR: true if either side is true |
| ! | logical NOT: flips true to false and false to true |
let age = 16;
console.log(age >= 13 && age <= 19); // a teenager?
When an expression has several operators, JavaScript follows an order, just like order of operations in math class. Multiplication and division happen before addition and subtraction; parentheses override everything:
console.log(2 + 3 * 4); // * first: 2 + 12
console.log((2 + 3) * 4); // parentheses first: 5 * 4
Which of these is a unary operator?
*typeof===+=typeof takes a single operand (typeof 42). The other three all need a value on each side.What do these log?
console.log("8" + 2);
console.log("8" - 2);
82 and 6. With a string involved, + joins. - has no string meaning, so it converts "8" to the number 8 and subtracts.Every comparison operator gives back which type of value?
true or false, no matter what is being compared."10" + 5, "10" - 5, and "10" * 5. Then log the typeof of each resultlet name = "Ada"; then use += to add a space and a last name. Log itlet x = 5; and log every comparison operator from the table against 8. Predict each answer first