0
0
Typescriptprogramming~15 mins

Declaring global variables in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Declaring Global Variables in TypeScript
📖 Scenario: You are building a simple TypeScript program that tracks the number of visitors to a website. You want to use a global variable to keep count so that it can be accessed and updated from different parts of your program.
🎯 Goal: Create and use a global variable in TypeScript to count website visitors.
📋 What You'll Learn
Declare a global variable called visitorCount with initial value 0.
Create a function called addVisitor that increases visitorCount by 1.
Call the addVisitor function three times.
Print the final value of visitorCount.
💡 Why This Matters
🌍 Real World
Global variables are useful when you want to keep track of information that many parts of your program need to access or update, like counting visitors on a website.
💼 Career
Understanding global variables helps you manage data that needs to be shared across different functions or modules in real-world TypeScript applications.
Progress0 / 4 steps
1
Declare the global variable
Declare a global variable called visitorCount and set it to 0.
Typescript
Need a hint?

Use the let keyword to declare a variable that can change later.

2
Create a function to add visitors
Create a function called addVisitor that increases the global variable visitorCount by 1.
Typescript
Need a hint?

Inside the function, use visitorCount += 1 to add one to the count.

3
Call the function multiple times
Call the function addVisitor exactly three times to increase visitorCount.
Typescript
Need a hint?

Call addVisitor() three times, each on its own line.

4
Print the final visitor count
Print the value of the global variable visitorCount using console.log.
Typescript
Need a hint?

Use console.log(visitorCount) to display the count.