FAQ: Iterators - Iterator Documentation

seems you need to use fat arrow function.


In step 3 of the exercise, I don’t understand why I get a different value of “true” or “false” depending on whether I use “return” in my “every” method. ie.:

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

gives the output: true


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

gives the output: false

Why does this happen?


When curly braces are used, return must be present.

array.every(x => x.length > 5)

Single expression, inline, implicit return.


Why does this return true? Because there are words that are equal to 5. I didn’t use ‘return word.length >= 5;’

const words = [‘unique’, ‘uncanny’, ‘pique’, ‘oxymoron’, ‘guise’];

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

You call the .every method on interestingWords array, not words array. interestingWords only contains word with a length greater then 5 due to the filter we applied in step 2:

const interestingWords = words.filter((word) => {return word.length > 5});

I’m confused by this statement. you can console.log an entire function? what is happening in this statement?
console.log(words.some(word =>
return word.length < 6;

When it is given as an expression we can log anything, including a function expression.


words.some()             =>  expression

word => word.length < 6  =>  also an expression

i have a question about the last part of the exercise.
when i don’t use the return command in the line

console.log (interestingWords.every(word => {
return word.length > 5
} ));
i get false.
why is that? all of the strings in the array are bigger than 5…

If you don’t use return, you get undefined (the absence of a return value) which is a falsy value:

console.log(undefined ? true : false)

When the callback is written in one statement or expression then the curly braces can be left off, as well as the return since it is implicit in that scenario.

___.every(x => x.length > 5)

When braces are used, the return must be explicit.

I am very confused at this point. My code is as follows:

const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];
const interestingWords = words.filter(word => word.length > 5);

This produces [ ‘unique’, ‘uncanny’, ‘oxymoron’ ], as expected

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

This produces false, which is not what I expected.
So I tried

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

and this produces true.
I do not understand why the extra {} in the above causes a problem. Can somebody please explain?

we can write a function the following way:

const example = (word) => {
   return word.length > 5

then there are a couple of shorthand. We can omit the return keyword but only if we also omit the curly brackets:

const example = (word) => word.length > 5;

this is only allowed when the body of the function is a single line.

we can also omit the brackets if our function has a single parameter:

const example = word => word.length > 5;

you make a weird mixture of some of these shorthands

that won’t work.

also, if return is missing you get undefined (absence of return value), which is falsy

1 Like

I’m a bit confused here?

const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];

// Something is missing in the method call below

console.log(words.some((word) => {
  return word.length < 6;

// Use filter to create a new array
const interestingWords = words.filter(word => word.length > 5);

// Make sure to uncomment the code below and fix the incorrect code before running it
console.log(interestingWords.every(word =>    word.length > 5    ));

Aren’t we doing the same thing in last two steps? We already filtered out array (words) with the same criteria. Now at the last step we are doing the same on already filtered out array.

.some and .every both return a boolean. .filter returns a list of values that pass the predicate function in its callback.

the following code gives a false input
console.log(interestingWords.every((word) => {

word.length > 5 ;

} ));
but when I write return word.length > 5; it returned a true value .
Why is it so ?

without the return keyword, you get undefined, which is a false value.

It has to do with the curly braces. There is no implicit return when they are in play.

So would it be safe if I just put return in every function?

Rather than imply it? Sure. But this language doesn’t press the point.

There are languages I’m sure that insist on every function having an explicit return. JS simply returns undefined if we omit it when not needed.

It can also serve as a signal for the bottom of a function block, if nothing more. When scanning it becomes something else for the eye to catch.


What we write after return is an expression. Say we want our function to log something. Do we write,



return console.log(something)

What’s neat here is that .log() returns the default, undefined and that is the anticipated return value at the caller.

We can return the function we are carrying out if it is not the function we’re using to do it. Though there is another concept which says we can use the same function in recursion. Returns are the backbone of that concept.

Let’s have two functions, one to add a to b, and one to subtract a from b.

addab = (a, b) => a + b

subab = (a, b) => b - a

f = x => x % 2 ? subab : addab

f(1)(6, 7)    // 1
f(0)(6, 7)    // 13

In the above we returned a function that could be invoked by the caller. The caller provided the state that would govern which function was returned. So long as we know the necessary arguments for that function, we’re good to go.

Functions are expressions, like any other value. We can pass them around, and now you’ve seen how return is actually very functional. Guess that pun fits in, but seriously, functions are the gears of our program. Return is a big part of that since it connects back to the caller. It is the handshake of that transaction.

Just to take it a bit further for stitches and giggles…

mulab = (a, b) => a * b 

divab = (a, b) => b / a 

f = x => [addab, subab, mulab, divab][x % 4]

f(42)(6, 7)    // 42

f(7)(6, 42)    // 7

JS is cool like that.

Man, did I get this question wrong! Had I read the post it was a reply to, the answer would be significantly different. My wires got seriously crossed. I think it is safe to say we need some clarification on the question as relates to iterators.

The above constructs play well as iterator callbacks so we’ve not had to toss the baby with the bath water. Still, some clarification of the question would do well at hand.