Assignment operators
Last updated
Last updated
An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal (=
), which assigns the value of its right operand to its left operand. That is, x = f()
is an assignment expression that assigns the value of f()
to x
.
If an expression evaluates to an , then the left-hand side of an assignment expression may make assignments to properties of that expression. For example:
For more information about objects, read .
If an expression does not evaluate to an object, then assignments to properties of that expression do not assign:
It is an error to assign values to unmodifiable properties or to properties of an expression without properties (null
or undefined
).
JSCopy to Clipboard
However, like other expressions, assignment expressions like x = f()
evaluate into a result value. Although this result value is usually not used, it can then be used by another expression.
By chaining or nesting an assignment expression, its result can itself be assigned to another variable. It can be logged, it can be put inside an array literal or function call, and so on.
The evaluation result matches the expression to the right of the =
sign in the "Meaning" column of the table above. That means that x = f()
evaluates into whatever f()
's result is, x += f()
evaluates into the resulting sum x + f()
, x **= f()
evaluates into the resulting power x ** y
, and so on.
In the case of logical assignments, x &&= f()
, x ||= f()
, and x ??= f()
, the return value is that of the logical operation without the assignment, so x && f()
, x || f()
, and x ?? f()
, respectively.
Note that, for all assignment operators other than =
itself, the resulting values are always based on the operands' values before the operation.
For example, assume that the following functions f
and g
and the variables x
and y
have been declared:
Consider these three examples:
Evaluation example 1
The assignment expression y = x = f()
starts to evaluate.
The y
on this assignment's left-hand side evaluates into a reference to the variable named y
.
The assignment expression x = f()
starts to evaluate.
The x
on this assignment's left-hand side evaluates into a reference to the variable named x
.
The function call f()
prints "F!" to the console and then evaluates to the number 2
.
That 2
result from f()
is assigned to x
.
The assignment expression x = f()
has now finished evaluating; its result is the new value of x
, which is 2
.
That 2
result in turn is also assigned to y
.
The assignment expression y = x = f()
has now finished evaluating; its result is the new value of y
– which happens to be 2
. x
and y
are assigned to 2
, and the console has printed "F!".
Evaluation example 2
y = [ f(), x = g() ]
also evaluates from left to right:
The assignment expression y = [ f(), x = g() ]
starts to evaluate.
The y
on this assignment's left-hand evaluates into a reference to the variable named y
.
The inner array literal [ f(), x = g() ]
starts to evaluate.
The function call f()
prints "F!" to the console and then evaluates to the number 2
.
The assignment expression x = g()
starts to evaluate.
The x
on this assignment's left-hand side evaluates into a reference to the variable named x
.
The function call g()
prints "G!" to the console and then evaluates to the number 3
.
That 3
result from g()
is assigned to x
.
The assignment expression x = g()
has now finished evaluating; its result is the new value of x
, which is 3
. That 3
result becomes the next element in the inner array literal (after the 2
from the f()
).
The inner array literal [ f(), x = g() ]
has now finished evaluating; its result is an array with two values: [ 2, 3 ]
.
That [ 2, 3 ]
array is now assigned to y
.
The assignment expression y = [ f(), x = g() ]
has now finished evaluating; its result is the new value of y
– which happens to be [ 2, 3 ]
. x
is now assigned to 3
, y
is now assigned to [ 2, 3 ]
, and the console has printed "F!" then "G!".
Evaluation example 3
The assignment expression x[f()] = g()
starts to evaluate.
The x[f()]
property access on this assignment's left-hand starts to evaluate.
The x
in this property access evaluates into a reference to the variable named x
.
Then the function call f()
prints "F!" to the console and then evaluates to the number 2
.
The x[f()]
property access on this assignment has now finished evaluating; its result is a variable property reference: x[2]
.
Then the function call g()
prints "G!" to the console and then evaluates to the number 3
.
The assignment expression x[f()] = g()
has now finished evaluating; its result is the new value of x[2]
– which happens to be 3
. x[2]
is now assigned to 3
, and the console has printed "F!" then "G!".
JSCopy to Clipboard
In , the code above throws, because one cannot assign properties to primitives.
For more complex assignments, the syntax is a JavaScript expression that makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.
In general, assignments are used within a variable declaration (i.e., with , , or ) or as standalone statements).
Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, some JavaScript style guides ). Nevertheless, assignment chaining and nesting may occur sometimes, so it is important to be able to understand how they work.
When chaining these expressions without parentheses or other grouping operators like array literals, the assignment expressions are grouped right to left (they are ), but they are evaluated left to right.
y = x = f()
is equivalent to y = (x = f())
, because the assignment operator =
is . However, it evaluates from left to right:
x[f()] = g()
also evaluates from left to right. (This example assumes that x
is already assigned to some object. For more information about objects, read .)
That 3
is now assigned to x[2]
. (This step will succeed only if x
is assigned to an .)
Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, ).
In particular, putting a variable chain in a , , or statement often does not work. Only the outermost/leftmost variable would get declared; other variables within the assignment chain are not declared by the const
/let
/var
statement. For example:
This statement seemingly declares the variables x
, y
, and z
. However, it only actually declares the variable z
. y
and x
are either invalid references to nonexistent variables (in ) or, worse, would implicitly create for x
and y
in .