Function Keyword

You can declare a function using the function keyword as follows

function double(number){
  return number * 2;
}

const double2 = function(number){
  return number * 2;
};

console.log(double(4)); // 8

console.log(double2(4)); // 8

 

Arrow Notation

These are similar functions that use the arrow notation:

// Drops the function keyword
// Adds => after the arguments
const double3 = (number) => {
  return number * 2;
};

const double4 = number => number * 2;

console.log(double3(4)); // 8

console.log(double4(4)); // 8

Some points to highlight about the double4 function:

  • because it has a single argument, the parenthesis surounding the number variable are dropped.
  • because the body of the function contains a single line, the return keyword is dropped and so are the curly brackets.