About `every` method

When I was working on the exercise(https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-iterators/lessons/javascript-iterators/exercises/documentation), the 3 mission asked to use .every to check if the elements in the array are all longer than 5, but when I put word.length>5, it returned a false, I was thinking, the arrow functions would automatically return the value, but apparently I was wrong, but I don’t know the reason.

I checked the cheat-sheet(https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-functions/cheatsheet) and I quote:

Arrow functions with a single expression can use the concise function body which returns the result of the expression without the return keyword.

My answer:

console.log(interestingWords.every((word) => {word.length>5} ));

When I added the return keyword, it returned a true:

console.log(interestingWords.every((word) => {return word.length>5} ));

Please help.

HI @henry.gao95
Welcome to the forums!

Arrow functions just use another syntax for writing a function.
These functions basically do the same:

function multiply(x) {
  return x * x;
multiply(3); // 9

const multiply = (x) => {
  return x * x;
multiply(3); // 9

and in short:

const multiply = x => x * x;
multiply(3); // 9

Whether an arrow function returns a value, a boolean, or does not return anything at all but pushes or logs something depends on what you define as the function body.

That in this case a boolean rather than the value is returned from the function is not defined by the function syntax, but by the method you use: every() always returns a boolean. The method filter() also takes a function to check if an array item fulfilled a condition, but instead of returning a boolean, it returns the value.

Hi @mirja_t ,

Thanks for the quick answer.

I get your point on this part, I do understand the part {word.length>5} are supposed to return a boolean instead of a value, but I am a little confusing about the keyword return. As for my code below

console.log(interestingWords.every((word) => {word.length>5} ));

Why do I have to add the return before {word.length>5} since I thought it will return without put this keyword on?

Because you use curly braces here.
The short form only assumes that it has to return the value if you don’t write the braces:

const interestingWords = ['hello', 'world!'];
console.log(interestingWords.every(word => word.length > 5 )); // false
1 Like