77

This recent video claims that EMCAScript 6 destructuring is already partially implemented in Node.JS. I have tried various examples (using v0.10.12 and the --harmony flag), such as

var [a, b] = [1, 2];

and

var {a: a, b: b} = {a: 1, b: 2};

to no avail. This ticket seems to suggest that destructuring is not yet supported in V8.

Is destructuring really partially implemented in Node.JS? What are snippets of code I can play with?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Randomblue
  • 112,777
  • 145
  • 353
  • 547

3 Answers3

91

Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed:

var [a, b] = [1, 2];

For older versions of node: You can get the list of supported harmony features by typing:

node --v8-options | grep harmony

node 5.x will give you:

--es_staging (enable all completed harmony features)
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped harmony fetaures)
--harmony_modules (enable "harmony modules" (in progress))
--harmony_regexps (enable "harmony regular expression extensions" (in progress))
--harmony_proxies (enable "harmony proxies" (in progress))
--harmony_sloppy_function (enable "harmony sloppy function block scoping" (in progress))
--harmony_sloppy_let (enable "harmony let in sloppy mode" (in progress))
--harmony_unicode_regexps (enable "harmony unicode regexps" (in progress))
--harmony_reflect (enable "harmony Reflect API" (in progress))
--harmony_destructuring (enable "harmony destructuring" (in progress))
--harmony_default_parameters (enable "harmony default parameters" (in progress))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
--harmony_atomics (enable "harmony atomics" (in progress))
--harmony_simd (enable "harmony simd" (in progress))
--harmony_array_includes (enable "harmony Array.prototype.includes")
--harmony_tostring (enable "harmony toString")
--harmony_concat_spreadable (enable "harmony isConcatSpreadable")
--harmony_rest_parameters (enable "harmony rest parameters")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_arrow_functions (enable "harmony arrow functions")
--harmony_new_target (enable "harmony new.target")
--harmony_object_observe (enable "harmony Object.observe")
--harmony_spreadcalls (enable "harmony spread-calls")
--harmony_spread_arrays (enable "harmony spread in array literals")
--harmony_object (enable "harmony Object methods")

The flag you need, --harmony_destructuring, was added in Node 4.1. Currently, you'll need to pass the --harmony_destructuring flag to enable the feature:

$ node --harmony_destructuring
> var {foo} = {foo: 'bar'};
undefined
> foo
'bar'
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49
  • 3
    @BrianMcCutchon Without `var`, you'll need to do `({foo} = {foo: 'bar})` to prevent the leading `{` from being interpreted as starting a block. That's the same in any implementation. –  May 23 '16 at 16:53
  • @torazaburo Interesting, hadn't thought of that. You're supposed to be able to omit the `var`/`let` when destructuring arrays, though, but that doesn't seem to work in node yet. – Brian McCutchon May 23 '16 at 17:14
15

The lately released node.js v6 is using V8 version 5.0 which is supporting 93% of ES2015 language features (even 96% in v6.1).

Destructuring assignments can now be considered stable and can be used without any flags.

birnbaum
  • 4,718
  • 28
  • 37
10

The ES6 compatibility table shows that destructuring isn't supported in either Chrome 45, or Node v4.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404