Arrow Functions

Named function

const sayHello = function(){
  console.log('Hello');
}
sayHello();

Arrow function

const sayHello = () =>{
  console.log('Hello Arrow');
}
sayHello();

One line function does not need braces

const sayHello = () => console.log('Simple Hello');

sayHello();

One line returns

const sayHello = () => 'Hello';

console.log(sayHello());

Return object

const sayHello = () => ({ msg: 'Hello'});

Single param does not need parenthesis

const sayHello = name => console.log(`Hello ${name}`);
sayHello('Gabor');

Multiple params need parenthesis

const sayHello = (firstName, lastName) => console.log(`Hello ${firstName} ${lastName}`);

sayHello('Gabor', 'Flamich');
const users = ['Nathan', 'John', 'William'];

const nameLengths = users.map(function(name){
  return name.length;
})
console.log(nameLengths);

// Shorter
const nameLengths = users.map((name) => {
  return name.length;
});

// Shortest
const nameLengths = users.map(name => name.length);
console.log(nameLengths)
Was this page helpful?

Reader Interactions

Leave a Reply

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