Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

The ES6 Way

Jan 28, 2019 · 1 min
The ES6 Way

Do you know the new and short hand syntax provided by new es6 standard. Here are the list of new syntax you can use right in you new javascript project now.

1. Default value for argument in any function:

Older way: function foo(x) { x = (typeof x != "undefined") ? x : 10; .. }

ES6 way: function foo(x = 10) { .. }

2. Argument in a function

Older way: function foo() { var args = [].slice.call(arguments); .. }

ES6 way: function foo(...args) { .. }

3. N Number of argument

Older way: function foo() { var args = [].slice.call(arguments); .. }

ES6 way: function foo(...args) { .. }

4. Auto initialized the object:

Older way: var o = { x: 2, y: 3 }, x = o.x, y = o.y, z = (typeof o.z != "undefined") ? o.z : 10;

ES6 way: var { x, y, z = 10 } = { x: 2, y: 3 };

5. Default initialization inside array:

Older way:

ES6 way: var a = [1,2], x = a[0], y = a[1], z = (typeof a[2] != "undefined") ? a[2] : 10;

ES6 way: var [x,y,z = 10] = [1,2];

6. Exchanging value witout temp variable

Older way: var x = 10, y = 20, tmp = x; x = y; y = tmp;

ES6 way: var x = 10, y = 20; [y,x] = [x,y];

7. Spread operator and its user.

Older way: var a = [1,2,3], b = a.pop(); a = a.concat([4]); a;

ES6 way: var a = [1,2,3], [b, ...a] = [...a,4];

8. Variable in argument

Older way: function foo(o) { var x = o.x, y = o.y; .. }; foo( {y:5,x:10} );

ES6 way: function foo({x,y}) { .. }; foo( {y:5,x:10} );