Declaring global variables in Typescript - Time & Space Complexity
When we declare global variables, we want to know how it affects the program's speed as it runs.
We ask: Does adding global variables make the program slower as it grows?
Analyze the time complexity of the following code snippet.
let count = 0;
function increment() {
count += 1;
}
increment();
increment();
This code declares a global variable count and a function that increases it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple variable assignment and addition.
- How many times: Each call to
increment()runs once, no loops or recursion.
Execution time stays about the same no matter how many global variables are declared.
| Input Size (number of global variables) | Approx. Operations |
|---|---|
| 10 | 10 simple assignments |
| 100 | 100 simple assignments |
| 1000 | 1000 simple assignments |
Pattern observation: Adding more global variables increases operations linearly, but each operation is very simple and does not repeat inside loops.
Time Complexity: O(n)
This means declaring or using global variables takes time proportional to the number of variables accessed or modified.
[X] Wrong: "Declaring many global variables makes the program slower because it has to manage all of them every time."
[OK] Correct: Each global variable is stored once and accessed directly; no repeated work happens just because they exist.
Understanding that global variables themselves do not add time cost helps you focus on what really affects performance, like loops or heavy calculations.
"What if we added a loop that updates a global variable many times? How would the time complexity change?"
Practice
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 AQuick Check:
Global variable = accessible everywhere [OK]
- Confusing global with local variables
- Thinking globals are immutable
- Assuming globals are only inside classes
Solution
Step 1: Identify global declaration syntax
Usingvaroutside any function declares a global variable in TypeScript.Step 2: Analyze options
var globalVar = 10; usesvarcorrectly. let globalVar = 10; usesletwhich 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 BQuick Check:
Use var outside functions for globals [OK]
- Using let or const for globals outside modules
- Declaring variables without initialization
- Confusing functions with variables
var count = 5;
function increment() {
count++;
}
increment();
console.log(count);Solution
Step 1: Understand variable scope and mutation
The variablecountis declared globally with value 5. The functionincrementincreases it by 1.Step 2: Trace code execution
Callingincrement()changescountfrom 5 to 6. Thenconsole.log(count)prints 6.Final Answer:
6 -> Option CQuick Check:
Global var incremented once = 6 [OK]
- Assuming count stays 5 after increment
- Thinking count is local inside function
- Expecting undefined due to scope confusion
export {};
declare global {
var message: string;
}
message = "Hello";
console.log(message);Solution
Step 1: Understand declare global usage
Thedeclare globalblock is used to add types to global variables but does not create or assign them.Step 2: Identify assignment error
Assigningmessage = "Hello";outside the block causes error becausemessageis only declared, not defined. You must define it separately.Final Answer:
Cannot assign to message outside declare global block -> Option DQuick Check:
declare global declares types, assignment needs separate definition [OK]
- Trying to assign inside declare global block
- Confusing declaration with definition
- Using let or const inside declare global
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?Solution
Step 1: Declare global variable type inside module
Usedeclare globalblock to declare the variable type for global scope.Step 2: Assign value to global variable correctly
Assign the value toglobalThis.appNameto 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 toglobalThis. 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 AQuick Check:
declare global + globalThis assignment = global var [OK]
- Assigning globals without globalThis
- Using export instead of global declaration
- Declaring local variables instead of globals
Ready to prove it?
Pass the test and this pattern moves unknown
