.forEach not working

From what I’ve notice, when using .forEach you write a function name between the “( )”, but in the app’s practice mode you can only write a function call and the code does not work. Am I doing something wrong or is there a bug in the practice mode?

I was just doing a quick practice, so don’t make fun of me for the second title :sweat_smile: :joy:

Hey there, good question! Let’s talk for a moment about identifiers and callback functions.

When you declare a function or a variable, you give it a name. Another word for this name is “identifier”. When the computer sees that identifier, it then looks in its memory for the variable or function associated with that name. For example,

let food = 'banana'; 
// food is the identifier for this variable

function sayHello() {
// sayHello is the identifier for this function

When we call a function, we use its identifier followed by (), with any arguments placed inside the (). The () is what tells the computer to actually run the function, like this:

sayHello() // this tells the computer to run the sayHello function

Array.forEach() takes a function as an argument, and then runs that function on each item in the array it is called on. This is called a “callback function”. When we use .forEach(), we want to tell it which function to run, but we’re not actually calling the function. .forEach() will call the function for us.

Because of this, we just give it the function’s identifier, without the (), like this:


Alternately, you can declare an “anonymous function” (meaning a function without an identifier) directly in the for each, like this:

// this is an anonymous function
bookshelf.forEach(function(book) {

// here it is written as an arrow function
bookshelf.forEach(book => console.log(book))

In your code, you’ll see the code keyboard has 2 keys for each function, one with a () and one without. Try deleting your function calls inside each .forEach(), and replacing them with the identifiers only, like this:


Hope this helps!

Thanks for this explanation… There is a problem though. In the app(practice mode) there is only the ones with “()” and not the ones without. I’ve look several times they are nowhere to be found.

Interesting, I hadn’t noticed that! Thanks for letting us know.

It looks like you can still create anonymous functions inside the .forEach() in the code playground. Alternately, you can use the code playground on web and practice typing out the code yourself.

No problem @Grasshopper_Ben

Hey, @Joandy. Good question. Let me tell you.

var, let or const statement creates an identifier, like this:
var sentence = “Hello World” (sentence is an identifier of this variable.)

Also, function keyword can still do this:
function sayHello() {
(sayHello is an identifier of this function.)

After this, I give you an example of an identifier without parentheses:

If there’s an identifier with parentheses, it will be look like this:

Also, you can create the annonymous function (A function without identifier).

Now, try remove these function calls and place identifiers without parentheses like this:

Hope this helps!