DEV Community

Jin Choo
Jin Choo

Posted on • Edited on

2 1

What is an Arrow Function? -Part 2

arrow functions

Implict Return

In JavaScript, an implicit return undefined occurs when the return statement is omitted from a function. The word implicit in this context refers to something that is implied but not directly stated. In other words, there was no return undefined as it would result in undefined.

Example:

const sum = (a, b) => {
   a + b;
}

sum(2, 5); // undefined
Enter fullscreen mode Exit fullscreen mode

In the above example, sum(2,5) returns undefined because we don't have the return keyword in the function body.

Example:

// implicit return
const sum = (a, b) => a + b;

sum(2, 5); // 7
Enter fullscreen mode Exit fullscreen mode

The return keyword and curly braces are not necessary if your arrow function is only one line long.

//without implicit return
const canVote = age => {
   return age >= 18;
}
Enter fullscreen mode Exit fullscreen mode

Let's utilize implicit return by:

  • removing the curly braces

  • removing the return keyword

const canVote = age => age >= 18;
Enter fullscreen mode Exit fullscreen mode

This is the clearest and shortest approach to write this function.

Array Methods Using Implicit Returns

Now let's see how to create the callback using arrow functions and implicit returns.

Array.filter()

//without implicit return
const ages = [10, 15, 21 45];

const agesAboveTwentyOne = ages.filter(function(age) {
   return age > 21;
});
console.log(agesAboveTwentyOne); //[21, 45]


//with implicit return
const agesAboveTwentyOne = ages.filter(age => age > 21);
Enter fullscreen mode Exit fullscreen mode

We filter the ages where the age is bigger than 21.

Array.find()

//without implicit return
const furBabies = ['Dutches', 'Monty', 'Benny', 'Lucky', 'Chili'];

const findNames = furBabies.find(function(furBaby) {
   return furBaby === 'Benny';
});
console.log(findNames); // 'Benny'


//with implicit return
const findNames = furBabies.find(furBaby => furBaby === 'Benny');
Enter fullscreen mode Exit fullscreen mode

From the furBabies array, find the furBaby that is equal to the string 'Benny'.

Array.map()

//Without implicit return
const numbers = [2, 6, 8, 10];

const tripled = numbers.map(function(number){
  return number * 3;
});
console.log(tripled); //[6, 18, 24, 30]


//with implicit return
const tripled = numbers.map(number => number *3);
Enter fullscreen mode Exit fullscreen mode

Create a new array based on the numbers array, where every number is multiplied by 3.

Please share your thoughts in the comments section below. I'd love to hear your thoughts. Or please drop by and say '👋'.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay