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

2024/2/27 9:21:44

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';

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');

Related Q&A

Prompting and preventing user from navigating away/closing a page

As per how StackOverflow does it when you are adding a question or replying to a question;Id like to prompt the user with a;"Are you sure you want to navigate away from this page" "Press…

How to check if JSON array is equal to

I am creating pie charts with JSON and Flot. The JS function to create the pie chart receives a JSON array from Django in this format:[1, 3, 2, 5, 4]If there is no data, the JSON array is:[0, 0, 0, 0, …

Clear $scope on logout in Angular js

In my controller I am storing data as $scope.$parent.dossierSummaries = data; but after log out and login the application $scope.$parent.dossierSummaries retains the same old data.I am doing this on lo…

How does node resolve a module location when its required from another module?

I have the following directories and files/path/to/dir1 /path/to/dir1/server.js /path/to/dir1/package.json /path/to/dir1/node_modules/path/to/dir2/moduleA.jsThen I have moduleA.js which starts like thi…

javascript not working in mozilla

<script type =javascript>function fun(userID) {var btn =; // error event undefine in mozilla alert(;} </script><asp:linkButton id ="target" style =" cu…

html onresizeend event, or equivalent way to detect end of resize

Im trying to detect when the onresize event ends in a browser. If I use the onresize event, in Firefox it seems to be fired only once, after the resize event ends, which is exactly what I want. But if …

Conversion object by namespace

I need to convert "flat object" like this (input data):{prop1: value.1,prop2-subprop1: value.2.1,prop2-subprop2: value.2.2, }to immersion object like this (output data):{prop1: value.1,prop2:…

why *ngIf in angular 2 always is executing when use function?

Im trying to create a application with angular 2,and have a auth service in my application , my html template is somthing like this:<header><div *ngIf="isLogin()"><a href="…

FormData.delete() doesnt work correctly inside a for loop

var data = new FormData(document.getElementsByTagName("form")[0]);for(var [key, value] of data.entries()) {if (!value) {data.delete(key);console.log(data.getAll(key));}}<form><input …

I dont know the scope of var

I want to make a table with vanilla js. But I got a different result depending on where I declare the var.var body = document.body;var table = document.createElement(table);var tr = document.createElem…