Vladimir Venegas Vladimir Venegas - 15 days ago 4
Javascript Question

What is the difference between function() and () => in javascript

i was wondering what is the differences between this:

Option A:

var elements = ['a', 'b', 'c', 'd'];
elements.forEach(function(e){
console.log('element is: ' + JSON.stringify(e, null, 2));
});


Option B:

var elements = ['a', 'b', 'c', 'd'];
elements.forEach((e) => {
console.log('element is: ' + JSON.stringify(e, null, 2));
});


Option B would be better/faster than A? Or is the same?

Thanks

Answer

Option B uses later syntax for javascript ES6 (also known as ES2015). It is only gradually being supported in browsers and node; usually if you want to use it currently you'll transpile it using something like Babel (this will effectively turn option B's code into option A's).

https://babeljs.io/docs/learn-es2015/

https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/

Comments