Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign two variables to the same value with one expression? [duplicate]

Is there any way to assign two variables to the same value?

Just tried this:

let x, y = 'hi'

and it's compiling to this:

'use strict';

var x = void 0,
    y = 'hi';
like image 364
ThomasReggi Avatar asked Dec 19 '22 17:12

ThomasReggi


1 Answers

Yes, it is possible:

let x, y;
x = y = 'hi';

It is called chaining assignment, making possible to assign a single value to multiple variables.
See more details about assignment operator.


If you have more than 2 variables, it's possible to use the array destructing assignment:

let [w, x, y, z] = Array(4).fill('hi');
like image 154
Dmitri Pavlutin Avatar answered May 23 '23 00:05

Dmitri Pavlutin