# Beginner JavaScript question from MDN curriculum

**URL:** <https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802>\
**Category:** MDN\
**Tags:** learning\
**Created:** [October 14, 2026, 11:02am UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802 "2026-10-14T11:02:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schooldude777](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@schooldude777](https://discourse.mozilla.org/u/schooldude777)\
**Post date:** [October 14, 2026, 11:02am UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/1 "2026-10-14T11:02:20Z")

</div>

Hello!

I’m new to JavaScript and I’m following the [MDN curriculum](https://developer.mozilla.org/en-US/docs/Learn_web_development) and I’m currently in [JavaScript: Adding interactivity](https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Your_first_website/Adding_interactivity) section.

Here is what I don’t fully understand. Code below:

```auto
const listItems = document.querySelectorAll("li");

function toggleDone(e) {
if (!e.target.className) {
e.target.className = "done";
} else {
e.target.className = "";
}
}

listItems.forEach((item) => {
item.addEventListener("click", toggleDone);
});

```

I don’t quite get where the “toggleDone” function get it’s value for the parameter “e”.

Is it this line: `item.addEventListener("click", toggleDone);` that passes the element to the parameter “e” (among other things)?

---

<div class="post-metadata">

**Author:** ![AC1DST4R](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ac1dst4r/32/78945_2.png) [@AC1DST4R](https://discourse.mozilla.org/u/AC1DST4R)\
**Post date:** [October 14, 2026, 12:18pm UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/2 "2026-10-14T12:18:20Z")

</div>

Hi @schooldude777 !

1. What does `addEventListener()` do?

Look at this line:

```javascript
item.addEventListener("click", toggleDone);

```

It tells the browser:

> “When someone clicks on this `item`, run the `toggleDone` function and give it information about the click.”

Notice that we’re passing `toggleDone`, **without parentheses**.

`toggleDone` means “here is the function to run later.”  
`toggleDone()` means “run the function right now.”

We want the first one because we’re waiting for a click.

2. Where does `e` get its value?

Look at your function:

```javascript
function toggleDone(e) {
    if (!e.target.className) {
        e.target.className = "done";
    } else {
        e.target.className = "";
    }
}

```

You haven’t assigned a value to `e` yourself. Instead, the browser supplies an event object automatically when it calls your function.

Here’s what happens when you click a list item:

1. You click a list item.

2. The browser detects the click.

3. The browser calls `toggleDone(eventObject)`.

4. The parameter `e` receives that event object.

5. Your function runs using the information in `e`.

Conceptually, it works like this:

```javascript
function toggleDone(e) {
    // e contains information about the click
}

// When a click occurs, the browser
// effectively does something like this:
toggleDone(eventObject);

```

`eventObject` is just a descriptive name here. You don’t need to create it yourself.

Best of luck, AC1DST4R

---

<div class="post-metadata">

**Author:** ![schooldude777](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@schooldude777](https://discourse.mozilla.org/u/schooldude777)\
**Post date:** [October 14, 2026, 5:42pm UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/3 "2026-10-14T17:42:37Z")

</div>

Thank you for the reply! Great information.

I have a follow up question:  
Where could’ve I get the information about the last part?

> “When someone clicks on this `item`, run the `toggleDone` function **and give it information about the click.** ”

I’m sure it’s there somewhere but I just didn’t see it. If what you wrote would’ve been in the docs it would’ve been so much clearer to me. I guess I was expecting there to be something like that or what you wrote later:

> Instead, the browser supplies an event object automatically when it calls your function.

I’m trying to become better at navigating and understanding the docs. That’s why I’m asking this.

---

<div class="post-metadata">

**Author:** ![AC1DST4R](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ac1dst4r/32/78945_2.png) [@AC1DST4R](https://discourse.mozilla.org/u/AC1DST4R)\
**Post date:** [October 14, 2026, 5:53pm UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/4 "2026-10-14T17:53:12Z")

</div>

That’s a great question! I think the information you’re looking for is in MDN’s **[Introduction to events](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Events)** guide, under the section titled `“Event objects.”`

That section explicitly explains that the browser automatically passes an event object to an event handler, and it demonstrates this with code very similar to yours:

```auto
function bgChange(e) {
    e.target.style.backgroundColor = "red";
}

btn.addEventListener("click", bgChange);

```

The key sentence in that section is that the event object is automatically passed to event handlers to provide additional information and functionality.

You can also find the more technical explanation in the [`addEventListener()` reference](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener), under **“`The event listener callback.”`** It explains that the callback receives an `Event` object describing the event that occurred.

I agree that this can be confusing when you’re starting out. The `addEventListener()` syntax tells you which function to register, but understanding what arguments that function receives requires knowing how event listeners work.

One useful distinction is that **`e`** isn’t a special JavaScript keyword. It’s simply the parameter name you chose for your function. The browser passes the event object to the function, and JavaScript makes that argument available through the first parameter, `e`. You could just as easily write `function toggleDone(event)` and use `event.target` instead.

When navigating documentation, I’d recommend looking at both the method’s reference page and the broader guide explaining the concept. The reference tells you how to use a method, while the guide often explains the underlying behavior and shows it in context.

Hope that helps, and keep asking questions like these! Understanding _why_ the code works is just as important as learning the syntax.

---

<div class="post-metadata">

**Author:** ![schooldude777](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@schooldude777](https://discourse.mozilla.org/u/schooldude777)\
**Post date:** [October 15, 2026, 12:42pm UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/5 "2026-10-15T12:42:32Z")

</div>

Thanks a ton @AC1DST4R for this reply! Just the kind of information I was looking for - very useful and practical!

I had a hunch that this topic will be covered in more detail later in the curriculum but I thought I would ask about it anyway. I’m very pleased to know that it will indeed be covered in the later parts!

Cheers!

---

<div class="post-metadata">

**Author:** ![whitescreen.dev](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/whitescreen.dev/32/78938_2.png) [@whitescreen.dev](https://discourse.mozilla.org/u/whitescreen.dev)\
**Post date:** [October 17, 2026, 5:02pm UTC](https://discourse.mozilla.org/t/beginner-javascript-question-from-mdn-curriculum/149802/6 "2026-10-17T17:02:27Z")

</div>

The MDN documentation for `addEventListener()` and the general guide on events are indeed the best places to find this information. Specifically, the “Event objects” section in the events guide and the “The event listener callback” part of the `addEventListener()` reference explain that the browser automatically passes an `Event` object to the handler function. This object contains details about the event that occurred, which is then accessible via the parameter you define (like `e` or `event`).
