es6 arrow functions and es5 functions comparison
es5 functions
[code language=”javascript”]
var fs = require("fs");
function add(x, y) {
return x + y;
}
var nums = [1,2,3,4,5];
var newNums = nums.map(function(n){return n*2});
console.log(add(3,10));
console.log(nums, newNums);
fs.readFile("./arrow_functions.js", "utf-8", function (err, data) {
if (err) console.log(err);
console.log(data);
});
[/code]
Equivalent functions written in es6 with arrow notation. It’s basically replacing the keyword function with the arrow =>
[code language=”javascript”]
let fs = require("fs");
let add = (x, y) => {return x + y;}
let nums = [1,2,3,4,5];
let newNums = nums.map(
(n) => {return n*2}
);
console.log(add(3,10));
console.log(nums, newNums);
fs.readFile("./arrow_functions.js", "utf-8", (err, data) => {
if (err) console.log(err);
console.log(data);
});
[/code]
Search within Codexpedia
Search the entire web