ReferenceError: word is not defined


#1

I’m working on an arrow function with the .filter() iterator:

let longFavoriteWords = favoriteWords.filter(word => word.length > 7)

This function returns SyntaxError: Identifier ‘longFavoriteWords’ has already been declared. Ands thats ok because it has already been declared.
But if it has been already been declared it means i can reference the variable without ‘let’. And when I do it:

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

I get the error: ReferenceError: word is not defined.
As far as I know, ‘word’ is a function parameter and it doesn’t have to be declared.

I think it works anyway but I would like to know the reason of the error.
Thanks.


#2

You’re giving us no context to work with. First, we should need the lesson page (link, please) and a bit more code to go on that just what you’ve given thus far. How about a full display of what you’ve got to this point?


#3

Tranks for your reply @mtf

Link to the lesson page: https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/filter?action=resume_content_item&program_content_id=0f24b3b442e0547eb392c75cea0e6efe&program_id=f48d2caf0f772f1018932221a5f0a005

Code:

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

let longFavoriteWords = favoriteWords.filter(function(word) {
  return word.length > 7;
});

// Refactor the code above using arrow function syntax

let longFavoriteWords = favoriteWords.filter(word => word.length > 7);
console.log(longFavoriteWords);

#4

When they say refactor it means adjust the code itself, or remove the old code.