Confused with this syntax - Basic Algorithm Scripting - Finders Keepers

on line 6 where the actual function call occurs, I’m a bit confused on the second argument.

num => num % 2 === 0

shouldn’t it be written

() => num % 2 === 0

or something along those lines? I’m having a hard time reading this and understanding what I have to do. This argument is throwing me off.

Your code so far

function findElement(arr, func) {
  let num = 0;
  return num;
}

findElement([1, 2, 3, 4], num => num % 2 === 0);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35

Challenge: Basic Algorithm Scripting - Finders Keepers

Link to the challenge:

This is a function definition.

num is the argument and num % 2 === 0 is the return value.

() would mean the function takes zero arguments.

1 Like
num => num % 2 === 0

This is arrow function syntax. It is basically the equivalent of:

function(num) {
  return num % 2 === 0;
}
1 Like

I totally forgot that if an arrow function only takes one parameter you can leave out the parenthesis. That completely threw me off.

A formatter like Prettier would add the parentheses.
https://prettier.io/playground

There is also an ESLint rule


Personally, I think it may not be a bad idea to enforce it.

  1. It lets you remove the argument/parameter and not break anything without having to add parentheses.

  2. It lets you add another argument/parameter without having to then add the parentheses.

  3. It can help readability, at least in some cases.