Saturday, February 7, 2026
HomeLanguagesJavascriptJavaScript Global Variables

JavaScript Global Variables

What are global variables in JavaScript?

JavaScript Global Variables can be accessed outside any function or block. JavaScript global variables are declared within the window object or outside any block or scope.

A variable declared without a keyword is also considered global even though it is declared in the function.

Below are some examples to see the declaration and use of JavaScript global variables.

 

Example 1: In this example, we will see the declaration of global variables and accessing them.

Javascript




// Global variable
let courseName = 'neveropen';
  
function myFunction() {
    console.log(courseName);
}
  
myFunction()


Output

neveropen

Example 2: In this example, we will declare a global variable inside a function and access it outside that function.

Javascript




function myFunction() {
  
    // Considered global
    courseName = 'neveropen';
    console.log( courseName );
}
  
myFunction();
  
console.log( courseName );


Output

neveropen
neveropen

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32493 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6864 POSTS0 COMMENTS
Nicole Veronica
11990 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12083 POSTS0 COMMENTS
Shaida Kate Naidoo
7000 POSTS0 COMMENTS
Ted Musemwa
7241 POSTS0 COMMENTS
Thapelo Manthata
6951 POSTS0 COMMENTS
Umr Jansen
6936 POSTS0 COMMENTS