How to Create a Global Variable in TypeScript ?
Last Updated :
28 Apr, 2025
To create a global variable in TypeScript, we have multiple approaches. In this article, we are going to learn how to create a global variable in TypeScript.
Below are the approaches used to create a global variable in TypeScript:
Using var, let
or const
at the Top Level
Declare a variable with var, let
or const
at the top level of your script.
Example: In this example, we are using var, let, and const to create a global variable in TypeScript.
JavaScript
var globalVar: string = "I am a global variable using var";
let globalLet: string = "I am a global variable using let";
const globalConst: number = 42;
function useGlobalVarLetAndConst() {
console.log(globalLet, globalConst, globalVar);
}
useGlobalVarLetAndConst(); // Accessible here
Output:
I am a global variable using let
42
I am a global variable using var
Using window
Object in Browser Environments
Assign a property to the window
object for global accessibility in browsers.
Example: In this example, we are using window
Object in Browser Environments to create a global variable in TypeScript.
JavaScript
// Using the window object in browser environments
(window as any).browserGlobal =
"I am a global variable accessible in browsers";
function useBrowserGlobal() {
console.log((window as any).browserGlobal);
}
useBrowserGlobal(); // Accessible here (in browser environments)
Output:
I am a global variable accessible in browsers
Using a Namespace or Module
Place the variable inside a namespace or module for organized global scope.
Example: In this example, we are using a Namespace or Module to create a global variable in TypeScript.
JavaScript
// Using a namespace or module
namespace GlobalNamespace {
export let globalVariable: string = "I am a global variable";
}
function useGlobalNamespace() {
console.log(GlobalNamespace.globalVariable);
}
useGlobalNamespace(); // Accessible here
Output:
I am a global variable
Using declare
Keyword
Use the declare
keyword for external global variables without a specific implementation.
Example: In this example, we are using declare
Keyword to create a global variable in TypeScript.
JavaScript
// Using declare keyword
declare var declaredGlobal: boolean;
function useDeclaredGlobal() {
console.log(declaredGlobal);
}
useDeclaredGlobal(); // Accessible here
Output:
The output will depend on the actual value assigned to externalLibraryVar
at runtime. For example, if it is defined externally before calling useExternalVariable()
, the output might look like this:
[declaredGlobal value]
If declaredGlobal is not defined, you might encounter a runtime error similar to:
Uncaught ReferenceError: declaredGlobal is not defined
Similar Reads
How to Declare Variables in TypeScript ? In TypeScript, a variable can be defined by specifying the data type of the value it is going to store. It will store the values of the specified data type only and throws an error if you try to store the value of any other data type. You can use the colon syntax(:) to declare a typed variable in Ty
2 min read
How to Get a Variable Type in TypeScript Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific
2 min read
How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab
2 min read
How to write a function in Typescript ? Writing a function in TypeScript is similar to writing it in JavaScript but with added parameters and return type. Note that any JavaScript function is a perfectly valid TypeScript function. However, we can do better by adding type.Syntax: Let's see a basic TypeScript function syntax (with two argum
4 min read
How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let
4 min read