Hi everyone.
According to MDN, when the only statement in an arrow function is return
, we can remove return
and remove the surrounding curly brackets, e.g.
const sum = (x,y)=>{return x + y;} // can be turned into
const sum = (x,y ) => x + y ;
Consider the following example
const array = [1, 2, 3, 4, 5];
console.log(array.some((element) => {return element % 2 === 0;}));
//output =true
as (element) => {return element % 2 === 0;} have one statement only , we can simplify it into (element) => element % 2 === 0;
But when I put it back to the example, error occur
console.log(array.some((element) => element % 2 === 0;));
//error: missing ) after argument list
However if I delete the ; after 0 , it works again
console.log(array.some((element) => element % 2 === 0));
//output =true
letting the function to x also works
let x = (element) => element % 2 === 0; // ; is NOT deleted in here
console.log(array.some(x))
//output =true
my question is
- Why deleting the ; works ? and
- Why letting the function ( the ; is NOT deleted) to x also works without deleting the ; ? Thank you. Sorry for my poor English