Assignment operators store values in variables. The basic one is =, which you have used since the first lesson. The rest are shortcuts for the most common thing programs do: take a variable, change it a bit, and store the result back in the same variable.
Given x = 10 and y = 5, here is what each operator leaves in x:
| Operator | Example | Same as | Result |
|---|---|---|---|
| = | x = y | x = y | x = 5 |
| += | x += y | x = x + y | x = 15 |
| -= | x -= y | x = x - y | x = 5 |
| *= | x *= y | x = x * y | x = 50 |
| /= | x /= y | x = x / y | x = 2 |
| %= | x %= y | x = x % y | x = 0 |
| **= | x **= y | x = x ** y | x = 100000 |
| &&= | x &&= y | assign y only if x is truthy | |
| ||= | x ||= y | assign y only if x is falsy | |
| ??= | x ??= y | assign y only if x is null or undefined | |
The first seven are the everyday ones. The last three are newer and need the idea of "truthy" and "falsy", explained further down.
= works out the value on its right and stores it in the variable on its left. The right side can be any expression:
let y = 4;
let x = 10 + y;
console.log(x);
An assignment is itself an expression: it produces the value that was assigned. That is what makes a chained assignment work, where the value is passed right to left:
let a, b;
a = b = 5; // b = 5 happens first and produces 5, then a = 5
console.log(a, b);
You will rarely need to write that yourself, but you will see it, and the same idea explains let y = x &&= 10 further down.
Each compound operator does its math using the variable's current value, then stores the result back in the same variable. Follow n through each line:
let n = 10;
n += 5; // n = 10 + 5 → 15
n -= 3; // n = 15 - 3 → 12
n *= 2; // n = 12 * 2 → 24
n /= 4; // n = 24 / 4 → 6
n %= 4; // n = 6 % 4 → 2
n **= 3; // n = 2 ** 3 → 8
console.log(n);
These show up everywhere in real programs:
// A running total, like a shopping cart
let total = 0;
total += 25.00;
total += 14.99;
total += 8.50;
console.log(total);
// Losing health in a game
let health = 100;
health -= 15;
health -= 15;
console.log(health);
// Doubling a score with a power-up
let score = 340;
score *= 2;
console.log(score);
x =+ 5 is not a typo JavaScript will catch. It reads as x = +5 (assign positive 5), so x becomes 5 instead of growing by 5. The operator symbol always comes before the equals sign: +=, -=, *=.
Two assignment operators work on strings: = stores a string, and += adds text onto the end of one. Building text up piece by piece is very common:
let text = "Hello";
text += ", ";
text += "World";
text += "!";
console.log(text);
The other compound operators (-=, *= and so on) have no meaning for text. On a string like "Hello" they give NaN.
The last three assignment operators decide whether to assign based on whether a value is truthy or falsy. When JavaScript needs a true/false answer from a value that is not a boolean, it treats a few special values as false. These are the falsy values, and there are exactly eight:
| Falsy value | What it is |
|---|---|
| false | The boolean itself |
| 0 | The number zero |
| -0 | Negative zero |
| 0n | BigInt zero |
| "" | An empty string (also '') |
| null | Empty on purpose |
| undefined | Never given a value |
| NaN | "Not a Number", from math that failed |
Everything else is truthy. That includes some values people often assume are falsy:
"0": a string containing a zero is not empty, so it is truthy"false": a string with letters in it is truthy[]: an empty array is truthy{}: an empty object is truthyTruthy and falsy come up again and again: in if statements, logical operators, and loops. JS Booleans covers them in depth.
These three operators only assign under a condition. They are handy for setting default values without writing an if.
x &&= 10 means "if x is truthy, replace it with 10; otherwise leave it alone":
| x starts as | after x &&= 10 | why |
|---|---|---|
| true | 10 | truthy, so assigned |
| 1 | 10 | truthy, so assigned |
| false | false | falsy, unchanged |
| 0 | 0 | falsy, unchanged |
| null | null | falsy, unchanged |
| undefined | undefined | falsy, unchanged |
Because an assignment produces a value, you can also capture the result: let y = x &&= 10; puts the final value of x into y as well.
x ||= 10 is the opposite: "if x is falsy, replace it with 10". Truthy values are left alone:
let a = false; a ||= 10; // 10
let b = null; b ||= 10; // 10
let c = undefined; c ||= 10; // 10
let d = true; d ||= 10; // stays true
let e = 5; e ||= 10; // stays 5
console.log(a, b, c, d, e);
x ??= 10 is stricter: it only replaces null or undefined, meaning "there is no value here yet". Every other value, including 0, "", and false, is kept:
let a; a ??= 10; // undefined, so 10
let b = null; b ??= 10; // null, so 10
let c = 0; c ??= 10; // stays 0
let d = 7; d ??= 10; // stays 7
console.log(a, b, c, d);
0 and you write score ||= 100 to set a starting default. Because 0 is falsy, ||= replaces the real score with 100. score ??= 100 keeps the 0. When 0, "", or false are valid values, use ??=.
Two more symbols often get listed alongside the assignment operators. You will learn each properly later, but here is what they look like so they do not surprise you.
The colon in objects. Inside an object's curly braces, a colon assigns a value to a property: {x: 45} creates an object whose x property holds 45. It is not a standalone operator; it only works in object syntax.
The spread operator ... splits something with many parts (an array, or a string's characters) into separate values:
let text = "12345";
console.log(Math.min(...text)); // like Math.min("1","2","3","4","5")
console.log(Math.max(...text));
Which line adds 5 to x?
x =+ 5;x += 5;x + 5;x == x + 5;x to positive 5. C works out x + 5 but throws the answer away. D is a comparison that is always false.What does this log?
let msg = "Level";
msg += " ";
msg += 3;
console.log(msg);
Level 3. msg is a string, so += joins, and the number 3 is turned into text.Which of these is truthy?
0"""0"null"0" is a string with one character in it, so it is not empty and it is truthy. The other three are all on the list of eight falsy values.score -= 10; run three times in all. Predict the result, then click Run+=, remembering the spaces. Log itlet x = 5; x =+ 3; and log x. Explain what happenedlet lives = 0; Try lives ||= 3 and log it. Reset to 0, try lives ??= 3, and compare