Codecademy Forums

FAQ: Iterators - The .filter() Method

This community-built FAQ covers the “The .filter() Method” exercise from the lesson “Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The .filter() Method

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For the second instruction, I used the following code:

const longFavoriteWorks = favoriteWords.filter(favoriteWords => favoriteWords.length > 7);

When I do a console.log, I see the output shown correctly, but the answer isn’t accepted. Am I doing something wrong, or am I doing this right and should this be accepted as a good answer?

Here’s what I entered as a solution to Instruction #2:

const longFavoriteWords = favoriteWords.filter(word => {
  return word.length > 7;

You’ve got a type-o in the variable you declared (‘longFavoriteWorks’… ‘k’ instead of ‘d’) in addition to the format difference I used.

Hope that helps.

1 Like

Sometimes there are functions (iterations and other functions) with a “return” statement inside the code block and sometimes without the “return”, but codecademy didn’t explain it properly in my opinion (or maybe I forgot, although it’s only been a week, tops). In one of the early lessons on functions, when the only thing I wrote inside the function body was console.log(‘Whatever’), I additionally printed an “undefined” to the console, in the line under ‘Whatever’. And now, even if I don’t involve “return” in my functions, I DON’T receive “undefined”… I don’t understand at all when I need to use “return” and when I don’t need it. Can somebody please explain?


const longFavoriteWords = favoriteWords.filter(word => {
return word.length > 7;


const longFavoriteWords = favoriteWords.filter(word => word.length > 7);

They both deliver the same result. However, writing the following:

const longFavoriteWords = favoriteWords.filter(word => return word.length > 7);

doesn’t work as intended.

Is the “return” statement just something that is needed when writing a function with a multi-line block?


The instructions read " The callback function is an arrow function has a single parameter, word ."

Shouldn’t they say " The callback function is uses fat arrow notation and the function has a single parameter, word ."?

Bad! They do not make any sense as stated?

I think the return keyword is not necessary when the code is condensed into a single line and will throw an error if you use return as you did in the last example:

const longFavoriteWords = favoriteWords.filter(word => return word.length > 7);

1 Like

The lesson on .filter() states the following:
“The callback function for the .filter() method should return true or false depending on the element that is passed to it.”
However, for the question “Which of the following methods returns a boolean value?”, ‘.filter()’ is NOT a correct choice.
Is the definition of .filter() on page 4 NOT describing boolean values?

Additionally, the lesson for .findIndex() states the following:
" Calling .findIndex() on an array will return the index of the first element that evaluates to true in the callback function."
However, .findIndex() is NOT a choice for the question “Which of the following methods returns a boolean value?”
What about .findIndex() evaluating to true or false is NOT boolean?

The return value of the .filter method is a list, determined by the callback. The callback is returning a boolean. But that is not what filter returns.

[1, 2, 3, 4, 5, 6, 7, 8, 9].filter(x => x % 2)

// [1, 3, 5, 7, 9]

The function, x => x % 2 will produce a 1 or a 0, which will be resolved to a boolean, true or false. Depending the outcome, the value of x will be inserted in the return list, or not. Recall that as an iterator, filter will iterate over the list it is called on.


Why is it “num” after .filter() instead of “randomNumbers”? In all other examples, the variable after .filter() is the same as the variable used for the original array.

randomNumbers is the array we are drawing our data from, num is one piece of data at a time. .filter iterates over the data, tests it against the condition and assigns the values that pass the test to the output array.

What names we use is completely arbitrary, though one would hope they make sense and carry some meaning for the reader. The computer doesn’t care about meaning, but anyone reading the code will definitely mind if the variables are just like something out of a Dr. Seuss story.

num is an appropriate name for the parameter in the callback. It is singular, which imparts information in and of itself. It tells us what form the data takes, a num, short for number. Notice that the arrays arre pluralized? That tells us there is a quantity of numbers in each array (or could be, at least).

1 Like

Take a look at the following example:

const words = [‘chair’, ‘music’, ‘pillow’, ‘brick’, ‘pen’, ‘door’];

const shortWords = words.filter(word => {
return word.length < 6;
words is an array that contains string elements.
const shortWords = declares a new variable that will store the returned array from invoking .filter().


The Array.filter() method returns a new array of values that satisfy the predicate in the callback. ‘Store’ would be a term that more closely describes this.

let shortWords = words.filter(word => word.length < 6)

Thank you. I do understand now after resetting the course, starting over and working back to here. Thank you for helping!!

1 Like

I wondered this myself. Why is ‘num’ the piece throw in there? Does the computer make the assumption were talking about numbers in the array versus say if I threw in another arbitrary variable like ‘try’ or ‘whatever’, instead of the variable ‘randomNumbers’?

When i replace ‘num’ with another arbitrary variable like ‘try’ , it returns an error. So it leaves me to assume the variable ‘num’ has some inherent value in the system, since num has not been declared as any other value in this iterator lesson? Is this not correct?

ANOTHER EXAMPLE would be the ‘word’ variable throw in on the previous exercise on this lesson:

const words = [‘chair’, ‘music’, ‘pillow’, ‘brick’, ‘pen’, ‘door’];
const shortWords = words.filter(word => {
return word.length < 6;

Why is ‘word’ used and not ‘shortWords’ ? ’ if I try to replace ‘word’ with another random variable, it returns an error. Does ‘word’ have an inherent value in this context as well that was not explained, or is it just another arbitrary word? Does ‘word’ assume the value of ‘shortWords’?

Not as a whole. It is only a temporary variable, taking one word at a time from the larger list. What we call it does not matter. However, if you replace the parameter, be sure to replace it in the object.length expression.

Why can’t I assign some other random name as that temporary variable? Why does only ‘words’ work in this equation without returning error? I understand why ‘words’ is used, it makes sense in context of this code, but I just dont understand why other random variable names dont work. Which is why I’m asking, is there some other value to ‘word’ im not aware of? Or is the fact the computer isn’t letting me input some other random variable name just a computer error?

let shortWords = words.filter(word => word.length < 6)

shortWords and words are defined data structures. The fact they are both pluralized speaks of there being some or many, not only one item in them.

The temporary variable is chosen as the singular of the list it is iterating.

words  =>  word

plural    singular

When viewed in such a way the code is readable and understandable. What other variable would make more sense?

As for choosing other names, we could write:

shortWords = words.filter(x => x.length < 6)

and get the same result. There is no error.

Is it the words variable you are trying to change? Then be sure the data structure it refers to is given the same name you wish to use in this statement.

content = story.split()

smallContent = content.filter(x => x.length < 6)

Computer errors only occur when something is encountered that cannot be found or a value is incorrect for its usage, such as a TypeError. It is our job to be sure these sorts of errors do not happen, and if they do, to trace them back to their cause.

Silly question - Why does this return words that has fewer than 6 characters, and not instead more than 6?

const words = [‘chair’, ‘music’, ‘pillow’, ‘brick’, ‘pen’, ‘door’];

const shortWords = words.filter(word => {
return word.length < 6;

The predicate is for less than 6. Only words with length that satisfies the predicate will be in the output array.

Perhaps it’s the Codecademy exercise interface that’s throwing you an error, not your code. Try your code in a program like Visual Studio Code and see if it works. I’m betting it will.