0
0
Javascriptprogramming~15 mins

Function scope in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Function scope
πŸ“– Scenario: Imagine you are organizing a small event and you want to keep track of the number of guests inside the event hall. You will use functions to manage this count, but you want to understand how variables inside and outside functions behave.
🎯 Goal: You will create a variable to count guests, write a function that changes this count inside its own scope, and then see how the count changes outside the function.
πŸ“‹ What You'll Learn
Create a variable called guestCount and set it to 10
Create a function called addGuests that declares a local variable guestCount and sets it to 20
Inside the function addGuests, print the local guestCount
Outside the function, print the original guestCount variable
πŸ’‘ Why This Matters
🌍 Real World
Understanding function scope helps avoid bugs when managing data in different parts of a program, like counting guests or tracking inventory.
πŸ’Ό Career
Many programming jobs require clear understanding of variable scope to write clean, bug-free code.
Progress0 / 4 steps
1
Create the initial guest count variable
Create a variable called guestCount and set it to 10.
Javascript
Need a hint?

Use let to create the variable guestCount and assign it the value 10.

2
Create a function with a local guestCount variable
Create a function called addGuests that declares a local variable guestCount and sets it to 20.
Javascript
Need a hint?

Inside the function addGuests, declare a new variable guestCount using let and assign it 20.

3
Print the local guestCount inside the function
Inside the function addGuests, add a line to print the local guestCount using console.log.
Javascript
Need a hint?

Use console.log(guestCount); inside the function to print the local variable.

4
Call the function and print the original guestCount
Call the function addGuests() and then print the original guestCount variable using console.log.
Javascript
Need a hint?

Call addGuests() first, then print guestCount with console.log(guestCount);.