SpinSpire logo

Arrow Functions/Lambda Expressions

Lambda Expressions/ Arrow Functions

// lambda expression 
(param1, param2) => {
// some code ...
}

This is SOOOOOO similar to ...

// anonymous function
function(param1, param2) {
// some code ...
}
(param1, param2) => expr; // where `expr` is ANY valid expression, such as (param1+param2)
param1 => expr; // a single parameter does not need parenthesis
() => expr; // zero parameter does need parenthesis
() => { // with a body
return expr; // equivalent to the previous example, the expression is the return value
};
const f1 = function (param) {
console.log("this", this);
console.log("param", param);
}

const f2 = (param) => {
console.log("this", this);
console.log("param", param);
}

// first param to `apply` is `this`, second param is an array of callee function parameters
f1.apply("THIS for f1", ["param-value-for-f1"]);
f2.apply("THIS for f2", ["param-value-for-f2"]);