This tutorial explains about following things.

  • Arrow Functions
  • Lambda Functions

ES6 Arrow Functions

ES6 introduced Arrow Functions syntax to create a new way of functions.

Arrow functions are used to declare anonymous functional expressions more quickly.

Lambda functions or arrow functions are similar but Let’s see the different.

Syntax:

()=>{expressionvalue}

The above is equal to the functional expression

function(){
    return expressionvalue;
}

Here is an example

Normal FunctionsArrow FunctionDescription
function(){}()=>{}No Arguments and no return types
function(m){}(m)=>{}Single Argument and not returned
function(m){return m}(m)=>mSingle Arguments with Return value
function(m, n){return m+n}(m, n )=>m+nMultiple arguments with return value
function(m, n){return m++;n++; return {m,n}(m, n)=>{m++;n++; m+nMultiple arguments, body contain statements, with return value

Arrow Functions in javascript

  • These functions have a shorter version of normal functional expressions. It is used when the body of a function is a single statement.
  • There is no own this binding context in these functions. this scope is derived from the surrounding code.

Let’s see multiple examples of array Function expressions.

  • Array functions without arguments

This example contains

- No arguments to expressions
- Body contains a single statement
- Return type is not required

Here is an example

const sayHello = () => console.log("Hello, Example");
sayHello(); // Output: Hello, Example

  • Simple Arrow functions with arguments

Arrow functions contain

  • arguments
  • body contains a single statement, return not required
const substract = (first, second) => first - second;
console.log(substract(20, 2)); // Output: 18
  • Arrow function expressions with multiple statements

Below functional expression below contains

  • Single parameter with lambda syntax
  • Body contains code statements enclosed in {}
  • Return is required to return the result in case of multiple lines in a body
const sum = (n) => {
  let result=0;
  for (let i = 1; i <= n; i++) {
  result+=i;
  }
  console.log(result);
  return result

};
sum(5);
  • Arrow functions with Object Literals Syntax

An object created using lambda function syntax.

  • Return not required
  • constructs an object in a single line, but It returns the data
const createStudent = (id, name) => ({ id, name });
console.log(createStudent(1, "Jonny")); // {"id": 1, "name": "Jonny"}

ES6 Lambda Functions

Generally, Lambda functions are functions without a name used in Functional programming

Arrow functions also called lambda functions in Javascript,

Here is a lambda function without a name.

const substract = function(first, second) {
  return first -  second;
};

Not all lambda functions are arrow functions.