30

Is there any way to perform multiple assignment in JavaScript like this:

var a, b = "one", "two";

which would be equivalent to this:

var a = "one";
var b = "two";
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
mlibre
  • 2,460
  • 3
  • 23
  • 32

5 Answers5

64

In ES6 you can do it this way:

var [a, b] = ["one", "two"];

The above code is ES6 notation and is called array destructuring/object destructuring (if it's an object).

You provide the array on the right-hand side of the expression and you have comma-separated variables surrounded by square brackets on the left-hand side.

The first variable maps to the first array value and so on.

nCardot
  • 5,992
  • 6
  • 47
  • 83
Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21
  • please add some more description to it, so that the answer is fully understandable – webdeb Jul 16 '16 at 13:50
  • 1
    If you are unsure about the technical term that you used here, then read and add it in your answer. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Rajaprabhu Aravindasamy Jul 16 '16 at 13:51
  • Its destructuring we can use array destructuring since we are talking about arrays – Piyush.kapoor Jul 16 '16 at 13:54
  • `var [a, b] = [1,2,3,4,5]` – webdeb Jul 16 '16 at 13:56
  • I'd remove the "object destructuring" part since it's a totally different syntax than the one presented, this may confuse beginners – nCardot May 01 '21 at 03:44
  • 2
    Will this incur any overhead from constructing the right-side array? Or are interpreters smart enough to have a fast path when the right side is an array literal? – Will Chen Jul 16 '21 at 14:51
12

While you cannot do var a, b = "one", "two";, and get them assigned to each variable in the respective order, you can do: var a, b; to initialize these variables as undefined.

You can also do var a, b = 'foo'; to assign b to 'foo', while initializing 'a' as undefined.

var a, b;

> a
--> undefined
> b
--> undefined
> var d, e;

> e
--> undefined

> f 
--> Uncaught ReferenceError: f is not defined

> var c, g = 'foo';

> c
--> undefined
> g
--> "foo"

Object destructuring

looks like:

const user = {
  id: 42,
  is_verified: true
};

const {id, is_verified} = user;

console.log(id); // 42
console.log(is_verified); // true 

You will also see this in JavaScript to import members of a module:

Import multiple exports from module. This inserts both foo and bar into the current scope.

import {foo, bar} from '/modules/my-module.js';
Danny
  • 3,982
  • 1
  • 34
  • 42
0

If you aren't absolutely married to the idea of the values being at the end of the statement, this works:

var a = "one", b = "two";

If you want to assign to variables that have already been declared, you can use the comma operator to make it a one-liner.

a = "ONE", b = "TWO";
Ryan
  • 1,053
  • 12
  • 21
0

I wanted to extend Ryan's answer - particularly when it comes to for loops. Normally one might write a for loop as:

for (var i = 0; i < arr.length; i++) {

As many contend, the interpretive nature of JS causes arr.length to be reevaluated on every loop - causing performance loss on large loops (depending on the nature of the upper-bound, e.g., a function). When performance is paramount, they suggest iterating backward:

for (var i = arr.length-1; i >= 0; i--) {

However, this approach will cause problems when order is relevant - not to mention being uncommon notation and unclear. To get around this problem, you can construct it thusly:

for (var i = 0, z = arr.length; i < z; i++) {

While not as succinct as a standard loop, it performs better whilest being easier to read than a reverse loop.

-13

No you can not do this way. Javascript doesn't support this type of declaration

Shobhit Walia
  • 496
  • 4
  • 19
  • 2
    Except that [it does](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). – nnnnnn Jul 16 '16 at 13:53
  • 2
    I seems you live in ice age of `JS` in ES6 you can use [Destructuring Assignment](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) – Morteza Tourani Jul 16 '16 at 14:06