Want to learn about arrow functions?

By | January 7, 2021

This is a neat video that explains very clearly what arrow functions are.

How does

function sum(a,b) {
  return a + b
}

become

let test = (a,b) => a + b

Here are the steps:

// Starting with:
function sum(a,b) {
  return a + b
}

// Step 1: remove function keyword coz it is assumed

sum(a,b) {
  return a + b
}

// Step 2: assign it a var with a let and make it equal what we have left over. (Normal functions assign a var with the actual name of the function)

let test = (a,b) {
  return a + b
}

// Step 3: add the arrow to denote a function (with a LHS & a RHS). DONE!

let test = (a,b) => {
  return a + b
}

Extra steps for 1 line functions.

// Step 4: Put on 1 line

let test = (a,b) => {return a + b}

// Step 5: Remove the return coz it is assumed

let test = (a,b) => {a + b}

// Step 6: Remove the curly braces

let test = (a,b) => a + b

Another advantage is when using anonymous functions.

But the main difference is when using the “this” keyword.

Leave a Reply

Your email address will not be published. Required fields are marked *