📖 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
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
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
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
Hint
Use console.log(visitorCount) to display the count.
Practice
(1/5)
1. What is a global variable in TypeScript?
easy
A. A variable accessible anywhere in the program
B. A variable only accessible inside a function
C. A variable that cannot be changed
D. A variable declared inside a class only
Solution
Step 1: Understand variable scope
Global variables are those that can be accessed from any part of the program, not limited to a function or block.
Step 2: Compare options
A variable accessible anywhere in the program correctly describes a global variable. Options B, C, and D describe other types or restrictions that do not match global variables.
Final Answer:
A variable accessible anywhere in the program -> Option A
Quick Check:
Global variable = accessible everywhere [OK]
Hint: Globals are variables you can use anywhere in your code [OK]
Common Mistakes:
Confusing global with local variables
Thinking globals are immutable
Assuming globals are only inside classes
2. Which of the following is the correct way to declare a global variable in TypeScript outside any function?
easy
A. let globalVar = 10;
B. var globalVar = 10;
C. const globalVar: number;
D. function globalVar() {}
Solution
Step 1: Identify global declaration syntax
Using var outside any function declares a global variable in TypeScript.
Step 2: Analyze options
var globalVar = 10; uses var correctly. let globalVar = 10; uses let which is block-scoped, not global. const globalVar: number; declares a constant without initialization, causing error. function globalVar() {} declares a function, not a variable.
Final Answer:
var globalVar = 10; -> Option B
Quick Check:
Use var outside functions for globals [OK]
Hint: Use var outside functions to create globals [OK]
Common Mistakes:
Using let or const for globals outside modules
Declaring variables without initialization
Confusing functions with variables
3. What will be the output of this TypeScript code?
var count = 5;
function increment() {
count++;
}
increment();
console.log(count);
medium
A. undefined
B. 5
C. 6
D. Error
Solution
Step 1: Understand variable scope and mutation
The variable count is declared globally with value 5. The function increment increases it by 1.
Step 2: Trace code execution
Calling increment() changes count from 5 to 6. Then console.log(count) prints 6.
Final Answer:
6 -> Option C
Quick Check:
Global var incremented once = 6 [OK]
Hint: Global var changes inside functions affect output [OK]
Common Mistakes:
Assuming count stays 5 after increment
Thinking count is local inside function
Expecting undefined due to scope confusion
4. Identify the error in this TypeScript code that tries to declare a global variable inside a module:
export {};
declare global {
var message: string;
}
message = "Hello";
console.log(message);
medium
A. Missing initialization of message inside declare global
B. declare global block must be inside a module file
C. message must be declared with let or const
D. Cannot assign to message outside declare global block
Solution
Step 1: Understand declare global usage
The declare global block is used to add types to global variables but does not create or assign them.
Step 2: Identify assignment error
Assigning message = "Hello"; outside the block causes error because message is only declared, not defined. You must define it separately.
Final Answer:
Cannot assign to message outside declare global block -> Option D
Quick Check:
declare global declares types, assignment needs separate definition [OK]
Hint: declare global only declares, assign globals separately [OK]
Common Mistakes:
Trying to assign inside declare global block
Confusing declaration with definition
Using let or const inside declare global
5. You want to add a global variable appName of type string in a TypeScript module and assign it the value "MyApp" so it can be used anywhere. Which is the correct way?
hard
A. declare global { var appName: string; }
globalThis.appName = "MyApp";
B. declare global { var appName: string; }
appName = "MyApp";
C. export var appName = "MyApp";
D. let appName: string = "MyApp";
Solution
Step 1: Declare global variable type inside module
Use declare global block to declare the variable type for global scope.
Step 2: Assign value to global variable correctly
Assign the value to globalThis.appName to define the global variable at runtime.
Step 3: Analyze options
declare global { var appName: string; }
globalThis.appName = "MyApp"; correctly declares and assigns the global variable. declare global { var appName: string; }
appName = "MyApp"; misses assigning to globalThis. export var appName = "MyApp"; exports a module variable, not global. let appName: string = "MyApp"; declares a local variable.
Final Answer:
declare global { var appName: string; }
globalThis.appName = "MyApp"; -> Option A
Quick Check:
declare global + globalThis assignment = global var [OK]
Hint: Use declare global plus globalThis to define globals in modules [OK]
Common Mistakes:
Assigning globals without globalThis
Using export instead of global declaration
Declaring local variables instead of globals
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.