Assignment Operators
JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
Oper | Example | Same As | Result | |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Try it » |
+= | x += y | x = x + y | x = 15 | Try it » |
-= | x -= y | x = x - y | x = 5 | Try it » |
*= | x *= y | x = x * y | x = 50 | Try it » |
/= | x /= y | x = x / y | x = 2 | Try it » |
%= | x %= y | x = x % y | x = 0 | Try it » |
: | x: 45 | size.x = 45 | x = 45 | Try it » |
Logical Assignment Operators
Oper | Example | Result | |
---|---|---|---|
&&= | true &&= 10 | x = 10 | Try it » |
||= | false ||= 10 | x = 10 | Try it » |
??= | null ??= 10 | x = 10 | Try it » |
The Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
Syntax | Example | |
---|---|---|
(condition) ? x : y | (age < 18) ? false : true | Try it » |
The Spread (...) Operator
The ...
operator splits iterables into individual elements.
Syntax | Example | |
---|---|---|
const myArr = [1, 2, 3]; | let xMin = Math.min(...myArr); | Try it » |
Learn More:
Study our JavaScript Assignment Tutorial.
Browser Support
Assignment
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
The Nullish Coalescing Operator (??)
The ??
operator returns the first argument if it is not nullish
(null
or undefined
).
Otherwise it returns the second argument.
Browser Support
??
is a JavaScript 2020 feature.
ES 2020 is supported in all modern browsers since September 2020:
Chrome 85 |
Edge 85 |
Firefox 79 |
Safari 14 |
Opera 71 |
Aug 2020 | Aug 2020 | Mar 2020 | Sep 2020 | Sep 2020 |