3

I'm getting an Uncaught SyntaxError: Invalid left-hand side in assignment error for

[d1,d2] =[d2,d1];

Anyone know why?

Sultan Shakir
  • 724
  • 2
  • 11
  • 22
  • 1
    What did you want to achieve with this statement? – Patrick Hofman Jun 16 '14 at 14:21
  • Here's a (present day) javascript compliant variable swap: http://stackoverflow.com/questions/16201656/how-to-swap-two-variables-in-javascript – bloodyKnuckles Jun 16 '14 at 14:21
  • 1
    Current JavaScript doesn't work like that. But it looks like ECMAScript 6 might. – Andy Jun 16 '14 at 14:22
  • 2
    This isn't supported by all (or afaik many at all) browsers. http://stackoverflow.com/questions/6983026/javascript-assign-array-values-to-multiple-variables – James Jun 16 '14 at 14:22
  • @PatrickHofman It is some code in a library I am using. It's in a function I probably won't use anyway since I don't foresee any need for it in the project I'm coding up. Thanks for the compliant code. Thank you all. – Sultan Shakir Jun 16 '14 at 15:00

2 Answers2

1

You have probably done a good try by looking at other posts here at SO, since your code looks much like the one at this comment.

Unfortunately, the feature you use runs with ECMAScript 6, and that isn't supported on all browsers (yet) as you can see here and more specific this one.

You should use the code in the answer this comment was under.

d2 = [d1, d1 = d2][0];
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    "ECMAScript isn't supported on all browsers" doesn't really make sense, because in present-day parlance JavaScript is an implementation of ECMAScript. Every common browser today supports at least ECMAScript 3 ([as JavaScript 1.5](http://en.wikipedia.org/wiki/JavaScript#Version_history)) and, as the chart you linked to shows, most of ECMAScript 5. (ES4 was "skipped" for political reasons.) It would be more accurate to say that destructuring assignment is an ECMAScript 6 draft specification feature and [isn't supported in all browsers](http://kangax.github.io/compat-table/es6/#Destructuring). – Jordan Running Jun 16 '14 at 14:39