How to declare Global Variables in JavaScript ?
Last Updated :
18 Mar, 2024
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 variables globally.
- Global variables can be accessed from any part of the program.
Note: If you assign a value to a variable and forgot to declare that variable, it will automatically be considered a global variable.
How to declare Global Variables in JavaScript?
In JavaScript, you can declare global variables by simply declaring them outside of any function or block scope. Variables declared in this way are accessible from anywhere within the script.
Here's how you declare global variables:
// Declare global variables outside of any function or block scope
var globalVar1 = "Hello";
let globalVar2 = "World";
const globalVar3 = "!";
Declaring global variable in JavaScript Examples
Example 1: Declaring Global Variables in JavaScript
Here, globalVar1
, globalVar2
, globalVar3
, globalVar4
, PI
, and WEBSITE_NAME
are declared as global variables and can be accessed from anywhere within the script.
JavaScript
// Declaring global variables using var keyword
var globalVar1 = "Hello";
var globalVar2 = "World";
// Declaring global variables using let keyword
let globalVar3 = "JavaScript";
let globalVar4 = "Example";
// Declaring global variables using const keyword
const PI = 3.14;
const WEBSITE_NAME = "My Website";
// Accessing and printing global variables
console.log(globalVar1 + " " + globalVar2); // Output: Hello World
console.log(globalVar3 + " " + globalVar4); // Output: JavaScript Example
console.log("The value of PI is: " + PI); // Output: The value of PI is: 3.14
console.log("Website Name: " + WEBSITE_NAME); // Output: Website Name: My Website
OutputHello World
JavaScript Example
The value of PI is: 3.14
Website Name: My Website
Example 2: Accessing and Modifying Global Variables in JavaScript
The code demonstrates accessing a global variable Marks
both within and outside a function. Initially set to 10
, it's accessed inside myFunction()
. Later, it's modified outside the function scope to 200
.
JavaScript
var Marks = 10;
// Declaring global variable outside the function
myFunction();
// Global variable accessed from
// Within a function
function myFunction() {
console.log("global value of Marks is: ", Marks);
}
// Changing value of global
// Variable from outside of function
{
Marks = 200;
console.log("local value of Marks is: ", Marks);
}
Outputglobal value of Marks is: 10
local value of Marks is: 200
Similar Reads
How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla
2 min read
Why to avoid global variables in JavaScript ? Global variables are variables that are declared or defined outside any functions in the script. This indicates that global variables can be accessed from anywhere in the particular script and are not restricted to functions or blocks. JavaScript global variables can also be declared from within a f
4 min read
Global and Local variables in JavaScript In JavaScript, understanding the difference between global and local variables is important for writing clean, maintainable, and error-free code. Variables can be declared with different scopes, affecting where and how they can be accessed. Global VariablesGlobal variables in JavaScript are those de
4 min read
How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
2 min read
How to declare variables in different ways in JavaScript? In JavaScript, variables can be declared using keywords like var, let, or const, each keyword is used in some specific conditions. Understanding these declarations is crucial for managing variable lifetimes and avoiding unintended side effects in code.Table of Content JavaScript varJavaScript letJav
2 min read
JavaScript Global Variables Global variables in JavaScript are variables that can be accessed and modified from anywhere in the code, regardless of the scope where they were declared. These variables are stored in the global object (window in browsers and global in Node.js). The declaration of global variables and accessing th
2 min read