Updated June 12, 2023
Introduction to TypeScript Global Variable
In TypeScript, a global variable is defined as one of the variable scopes for declaring a variable in the TypeScript program where these global variables are variables declared outside all the program constructs like methods, constructors, etc. where it is defined and declared at the beginning of the program which the entire program can use this global variable till the end hence such variables that are available and accessible to the entire program are known as global variables. In general, global variables are variables declared globally, which means they are declared at the beginning of the program so that they are accessible to all the constructs of the program without declaring the variable again and again.
Working of Global Variables in TypeScript with Examples
In this article, we will see global variables in TypeScript with examples which are defined as a variable with global scope which is defined globally that is outside the function or block of code or class where these variables can be used in any part of the program from the beginning of the program till the end of the program. Hence, global variables are variables defined as a globally available variable and accessible to every part of the program that includes any block of code or functions or class, and such variables are defined in the global scope of the variable.
In TypeScript, variables are of 3 different scopes such as local scope, global scope, and class scope. Here, the global variables are declared in the global scope and can be accessed variable anywhere within the program known as a global variable. In TypeScript, there is no keyword for declaring global variables, such as the “global” keyword in other programming languages. Sometimes in TypeScript, they use the keyword “declare” to declare variables such as “declare var” for variables if it is a read-only variable if the variable is based on block scope, then we use “declare let”, which is sometimes used for declaring the global variables which are said to be in the global scope. But even “declare var” also works for declaring global variables.
There is no such syntax to be followed as these variables are always declared at the beginning of the program.
Let us see the view sample syntax for declaring a global variable.
Syntax:
var global_var = value;
class sample_class
{
Set of instructions or constructors or methods;
}
In the above syntax, we can see a variable named “global_var” is a global variable which is declared outside the class declaration which can be used anywhere within this program even within the class or constructor or methods which are in the same program that of a global variable and also can be accessed by anywhere within the program.
Example of TypeScript Global Variable
Different example are mentioned below:
Example #1
Code:
var a =42;
var b=53;
class add
{
addNum():void {
var res = a+b;
console.log("The addition of two variables result in: ",+res);
}
}
console.log("Demonstration of global variables in typescript");
console.log("The first global variable value declared is: ",+a);
console.log("The second global variable value declared is: ",b);
var obj = new add();
obj.addNum();
Output:
In the above program, we have first declared variables “a” and “b”, which are global variables. Then we have created a class “add” where we are defining ad declaring the function “addNum” within the class. We are calculating the addition of the above two variable values, and these values are accessible as the variables declared are in the global scope and hence we can use the same variable to calculate the result and then we can even print the values of each variable which we are printing at the end of the program here also the values of the variables are accessed and printed as they are globally declared if these variables were declared within the class. If we were trying to print the values of the given variable, it would give us an error; else, we would have to create a class object and then refer to the values of those variables. So in the above program, we can see the output in the above screenshot, showing how and where the global variables are used.
We will see another example where we will see all other variable scopes, such as global variable, local variable, class variable, etc.
Example #2
Code:
var global_var = "Educba"
class strclass {
c_val = "Python";
func():void {
var l_var = "Perl";
console.log("The local variable value is: "+l_var)
}
}
console.log("Demonstration of global variables in Typescript")
console.log("The global variable value is: "+global_var)
var obj = new strclass();
obj.func()
console.log("The class variable value is : "+obj.c_val)
Output:
In the above program, we can see we have first declared a variable outside the class “strclass” and this variable is “global_var” is a global variable and then we are declaring a class variable which within the class such as “c_val” which is within the class scope and then we are declaring a local variable within the method “func()” such as “l_var” and this variable is declared within the function and hence it is in the local scope. Therefore in the above program, we can observe when we are printing a global variable, we don’t need to use any period operator to access as it is in the global scope, later when we are trying to print a local variable which is within the class’s function and hence we first need to instantiate the object of the class as “obj” and then to access the local variable we need to call the function using the period operator (.) with “obj” to access the value of the local variable and then similarly to access class variable we have to use again the period operator (.) to refer or to point to the class variable using “obj” variable where the object is created. The output of the above program can be seen in the above screenshot.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Global Variable” was beneficial to you. You can view EDUCBA’s recommended articles for more information.