Vladimir Venegas Vladimir Venegas - 1 year ago 80
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'];
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?


Answer Source

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).



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download