Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Each Call Gets Its Own Variables

In this video, you saw why a counter declared outside a function gives a wrong count on the second call, and how declaring it inside the function fixes that. Here’s what to keep from it.

Key Points

  • When the counter is declared outside the function, every call shares that one count. The delivery call leaves it at 2, so the billing call starts from 2 and reports 3 instead of 1. Nothing crashes, so the wrong number can slip into a report.
  • The fix is to move one line: declare let count = 0; inside the function, at the top of its body.
  • Each call gets its own variables, like a fresh room: the parameters messages and topic hold that call’s arguments, and count starts at 0. When the call returns, the room closes and its count is gone.
  • count is declared inside countTopic(), so it can only be used inside that function’s braces. The code that calls the function can’t read it, so a line after the calls that logs count stops with ReferenceError: count is not defined. To use a count outside, return it and keep it in deliveryCount or billingCount.
  • The rule: declare a variable inside the function that needs it, and every call gets a fresh one. When you read a reporting function, whoever wrote it, check where each counter is declared.

Final Code

Language: JavaScript
const messages = [
  { id: "M-101", topic: "delivery", text: "Box came late" },
  { id: "M-102", topic: "billing", text: "Charged twice" },
  { id: "M-103", topic: "delivery", text: "Wrong address" },
];

function countTopic(messages, topic) {
  let count = 0;
  for (const message of messages) {
    if (message.topic === topic) {
      count = count + 1;
    }
  }
  return count;
}

const deliveryCount = countTopic(messages, "delivery");
const billingCount = countTopic(messages, "billing");
console.log("delivery", deliveryCount);
console.log("billing", billingCount);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 In the last lesson, your countTopic function learned to hand its count back instead of logging it. Now each report needs to stay correct, however many reports you run.

00:15 Here’s a version of countTopic from a teammate’s report. It’s the same as yours, except the counter line sits above the function, outside its body. The program then calls the function twice, once for delivery and once for billing, and logs both counts.

00:35 The data has three messages: two about delivery and one about billing. Your turn: pause the video and predict what the billing line will show.

00:50 The billing line shows 3, not 1. Whoops, that isn’t the count you wanted. There’s only one count in this program, and it lives outside the function. The delivery call adds two to it, so it’s already 2 when the billing call starts.

01:10 The billing call adds one more and returns 3. Nothing crashed, and nothing looks broken. A report like this could go out with the wrong number. The fix is to move one line.

01:27 Put the declaration of count inside the function, at the top of its body.

01:34 Now every call gets its own variables. Think of each call as getting a fresh room, like the room with a front door from the last lesson. When the delivery call starts, JavaScript opens a room for it.

01:49 Inside sit messages and topic, holding this call’s arguments, and a new count that starts at 0. The loop raises count to 1, then 2. Return hands the 2 out through the front door.

02:05 Then the room closes, and its count is gone.

02:09 The billing call opens a new room, with its own count starting at 0 again. One match later, it returns 1. When the program runs now, the output shows delivery 2 and billing 1.

02:25 Each report counts only its own topic, however many calls come before it.

02:32 Watch out for one more thing. The count variable is declared inside countTopic, so it can only be used inside that function’s braces. The code that calls the function can’t read it at all.

02:48 So a line after the calls that logs count stops with an error: count is not defined. To use a count outside, return it and keep it in deliveryCount or billingCount.

03:01 That’s the rule. Declare a variable inside the function that needs it, and every call gets a fresh one.

03:10 When you read a reporting function, whether you wrote it or an AI assistant did, check where each counter is declared.

03:19 Next up is the exercise Keep Two Reports Independent. There, you’ll move declarations so that two topic counts stay separate.

Become a Member to join the conversation.