Skip to content Skip to sidebar Skip to footer

Nodejs - Arrays On The Left Side

The following code works on browsers but not in Node.js. Why? [a, b] = 'hey,there'.split(','); console.log(a);

Solution 1:

ES6 allows something called destructuring assignments. (Nicely explained here. And here is a link to the draft spec. ) That's what you've got in your code.

It looks like it is an ES6 feature that Firefox has adopted early. (Maybe too early). Unfortunately, this is par for the course with JavaScript runtimes. Many of them give access to features from upcoming specs ahead of time. You've got to keep your eyes peeled to make sure you're not using something that has not quite yet been officially adopted.

Solution 2:

Your code is considered in ECMAscript 6.This is called array destructuring. You can see its features here https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7

your code will run on current version of Firefox (28) but not Google chrome (33) you can check the compatibility of you browser from this site http://kangax.github.io/es5-compat-table/es6/

If you want to run it in Google-chrome there's a flag named Enable Experimental JavaScript appeared in the chrome://flags ==> make this flag = true then you can run it

NodeJs support ECMASript 6 but you will run it using the following command

node --harmony yourapp.js

Post a Comment for "Nodejs - Arrays On The Left Side"