How to declare a global variable in Deno
Global variables help us to share the common data across the modules in our app. In this tutorial, we are going to learn about how to declare and use the global variables in Deno.
Declaring a Global Variable
We can declare a global variable in deno, by using the window
global object.
Here is an example, that declares the name
global variable.
window.name = "Movies app";
Using the Global Variable
Now, we can use the name
global variable inside the other modules of our app like this.
console.log(name); // "Movies app"
Declaring a Global variable in TypeScript
If you are using TypeScript as a runtime in deno, you need to extend the window interface by specifying a new property before declaring the global variable.
Example:
declare global {
var name: any;
interface Window {
name: any;
}
}
window.name = "Movies app";