47

This is valid in php:

$x=$y='value';

This will in esscence set both $x and $y to 'value'.

Is this valid in javascript?

var x=y='value';

I have tested it in chrome console and it worked as expected, but just wanted to double check before starting to use it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
  • 1
    possible duplicate of [How to define multiple variables on a single line?](http://stackoverflow.com/questions/4166785/how-to-define-multiple-variables-on-a-single-line) – Lightness Races in Orbit Sep 28 '11 at 10:06

6 Answers6

80

It only works if the var y as been previously defined, otherwise ywill be global.

In such case, you better do:

var x, y;
x = y = 'value';

Assignments Chaining

Another antipattern that creates implied globals is to chain assignments as part of a var declaration. In the following snippet, a is local but b becomes global, which is probably not what you meant to do:

// antipattern, do not use
function foo() {
   var a = b = 0;
   // ...
}

If you’re wondering why that happens, it’s because of the right-to-left evaluation. First, the expression b = 0 is evaluated and in this case b is not declared. The return value of this expression is 0, and it’s assigned to the new local variable declared with var a. In other words, it’s as if you’ve typed:

var a = (b = 0);

If you’ve already declared the variables, chaining assignments is fine and doesn’t create unexpected globals. Example:

function foo() {
   var a, b;
   // ...
   a = b = 0; // both local
}

“JavaScript Patterns, by Stoyan Stefanov (O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.”

blong
  • 2,815
  • 8
  • 44
  • 110
user278064
  • 9,982
  • 1
  • 33
  • 46
14

I'm pretty sure that the most readable form is the one that has not been brought up yet:

let x = 0, y = 0
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
8

To prevent the y from becoming a global variable, use the following:

var x, y = x = 'value';

Apparently the declarations are simply evaluated from left to right.

Dykam
  • 10,190
  • 4
  • 27
  • 32
1

Yes, that is valid in Javascript.

However, after googling the problem, this Stack Overflow question explains some caveats, so use with caution.

Community
  • 1
  • 1
tonycoupland
  • 4,127
  • 1
  • 28
  • 27
0

When vars have different values you can easily write:

var x = 0,
    y = 1,
    ...
    z = 2;
maguri
  • 386
  • 2
  • 10
0

In some cases involving objects and arrays, Assignments Chaining is not safe.

This is an example:

    const a = {}
  // Use Assignments Chaining to save some typing.
  a['foo'] = a['bar'] = []

  // I just want to set a['foo'] when some condition meets.
  for (let i = 0; i < 5; i++) {
    a['foo'].push({ title: 'hello', number: i / 2 })
  }

  // But you maybe get this unexpected assignment.
  console.log(a['bar']) 
/*
  [
    {
      "title": "hello",
      "number": 0
    },
    {
      "title": "hello",
      "number": 0.5
    },
    {
      "title": "hello",
      "number": 1
    },
    {
      "title": "hello",
      "number": 1.5
    },
    {
      "title": "hello",
      "number": 2
    }
  ]
*/
hustnzj
  • 525
  • 6
  • 13