0
0
Javascriptprogramming~15 mins

Global scope in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Global Scope in JavaScript
πŸ“– Scenario: Imagine you are creating a simple web page that tracks the number of visitors. You want to store the visitor count in a place that can be accessed anywhere in your script.
🎯 Goal: You will create a global variable to hold the visitor count, then write code to update and display this count.
πŸ“‹ What You'll Learn
Create a global variable named visitorCount with the initial value 0.
Create a variable named newVisitors and set it to 5.
Add newVisitors to visitorCount using a simple addition.
Print the final value of visitorCount using console.log.
πŸ’‘ Why This Matters
🌍 Real World
Global variables are useful for storing information that many parts of a program need to access, like user settings or counters.
πŸ’Ό Career
Understanding global scope helps you avoid bugs and write clearer code, which is important for any programming job.
Progress0 / 4 steps
1
Create a global variable
Create a global variable called visitorCount and set it to 0.
Javascript
Need a hint?

Use let outside any function to create a global variable.

2
Add new visitors count
Create a variable called newVisitors and set it to 5.
Javascript
Need a hint?

Use let to create the variable newVisitors.

3
Update the global visitor count
Add the value of newVisitors to the global variable visitorCount using visitorCount = visitorCount + newVisitors.
Javascript
Need a hint?

Use the assignment operator = to update visitorCount.

4
Display the visitor count
Use console.log(visitorCount) to print the final value of visitorCount.
Javascript
Need a hint?

Use console.log to show the value in the console.