18

Is there a javascript equivalent to unpack sequences like in python?

a, b = (1, 2)
tback
  • 11,138
  • 7
  • 47
  • 71

3 Answers3

15
[a, b] = [1, 2]

Update:

Browser compatibility matrix:

  • Firefox: all versions
  • Opera: 9.x only
  • Chrome: 49 and higher
  • MSIE: no
  • EdgeHTML: 14 (Browser version 31, released Feb. 2016)
  • Safari: 7.1 (8 for Safari Mobile)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • why? which implementations are affected? do you have further links? – tback Jan 20 '10 at 11:00
  • 3
    In opera 10 works, in ff 3 works, in ie 8 doesn't, in chrome doesn't. Not universal indeed. – Antony Hatchkins Jan 20 '10 at 11:05
  • 2
    Well, for starters this code won't work in IE, Chrome or Safari implementations. I'm pretty sure major browser-wise this is only supported by Firefox and Opera. – Andy E Jan 20 '10 at 11:05
  • 8
    @Alsciende I'm tempted to flag you comment as offensive for mentioning IE6 ;) – tback Jan 20 '10 at 11:06
8

There isn't. JavaScript doesn't have such syntax sugar.

Marcin
  • 7,874
  • 7
  • 45
  • 49
  • 1
    this answer is less correct than mine. The accepted answer should be 'among modern browsers only Firefox has this feature'. – Antony Hatchkins Apr 24 '14 at 04:07
  • @AntonyHatchkins and Marcin, it's in ECMAScript 2015: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – leewz Sep 23 '15 at 00:51
  • This answer is no longer correct. See @AntonyHatchkins's answer. – noamtm Sep 10 '18 at 10:23
-4

An object cannot contain a reference to an integer, only its value. So I can't see any way to do what you ask in javascript.

Alsciende
  • 26,583
  • 9
  • 51
  • 67