Sometimes, we want to make multiple left-hand assignments with JavaScript.
In this article, we’ll look at how to make multiple left-hand assignments with JavaScript.
How to make multiple left-hand assignments with JavaScript?
To make multiple left-hand assignments with JavaScript, we first declare all the variables.
And then we can chain the assignments with =
.
For instance, we write
let v1, v2, v3;
v1 = v2 = v3 = 200;
to define the v1
, v2
and v3
variables with let
.
Then we assign 200 to v3
.
Then we assign v3
to v2
.
And then we assign v2
to v1
in the last line.
Using let v1, v2, v3;
lets us make sure that all the variables are block scoped.
Conclusion
To make multiple left-hand assignments with JavaScript, we first declare all the variables.