JavaScript .filter iterator

I’m currently on the .filter() exercise in the iterator section of the JavaScript course.

My code is fine but after completing the exercise I played around a bit to see if I could streamline the code and something happened that I can’t explain.

Here is the original code as it’s supposed to be in the exercise:

const favoriteWords = [‘nostalgia’, ‘hyperbole’, ‘fervent’, ‘esoteric’, ‘serene’];

const longFavoriteWords = favoriteWords.filter(word =>{

return word.length > 7})

Obviously you can then log the result using


giving nostalgia, hyperbole, and esoteric.

I wanted to see if I could include the console.log part in the code block so I changed it to this:

const longFavoriteWords = favoriteWords.filter(word =>{

console.log(word.length > 7)

I was expecting to then have to call longFavoriteWords to see anything in the console, but it actually logged this right away without me doing anything else:


I don’t understand why it did this - all I did was replace return with console.log, so why didn’t it just log the same set of words as when I used console.log(longFavoriteWords) after the original code? Where in my new code does it tell it to log the truthiness? And anyway, why did it log anything at all when I hadn’t called longFavoriteWords yet - if the console.log() is inside the body of the longFavoriteWords function, how can it run without me calling the function?

Sorry if my terminology is wrong, I’m finding it quite hard to explain what I’m confused about here.

Hey @isrtcify,
When you use .filter() on an array, it filters through each item in the array, and returns each item that meets the requirement, saving it in a new array.
The return statement you used returned the value of the array items (in this case they were strings) that met the requirement, saving them to the new array longFavoriteWords, but the console.log statement you used only returned whether or not it met the requirement, which is why it says true true false true false. The first, second, and fourth words (nostalgia, hyperbole, and esoteric) met the requirement, white the third and fifth words (fervent and serene) didn’t.
If you want to make it log the words inside the filter method, try something like this:

const favoriteWords = ['nostalgia', 'hyperbole', 'fervent', 'esoteric', 'serene'];

const longFavoriteWords = favoriteWords.forEach(word =>{

  if (word.length > 7) {


Similarly, you can use methods like .forEach() and .every() to iterate through each item in an array.
.forEach will go through every item and execute the code inside the method, while .every() returns a Boolean (true or false) value of whether or not every item in the array meets the requirement.

Check out MDN’s article on the .filter() method here: