How to define global variable in JavaSscript? [SOLVED]


Getting started with JavaScript Global Variables

JavaScript is a powerful programming language that is widely used for web development, and it offers a variety of features that allow developers to create dynamic and interactive web pages. One of the most important features of JavaScript is the ability to define variables, which are used to store and manipulate data within the program.

In JavaScript, a global variable is a variable that is defined outside of any function or block of code, and it can be accessed and modified by any other part of the program. When a variable is defined as a global variable, it is stored in the global scope, which means that it is accessible to all functions and code blocks within the program.


Defining Global Variables

To define a global variable in JavaScript, simply declare the variable using the var keyword, followed by the name of the variable. Here's an example of how to define a global variable called store:

var store = 23;

It is also possible to define a global variable without the "var" keyword, like this:

var store = 23;
store = 23;

This will automatically create a global variable, which is accessible throughout the entire program.


Using Global Variables

Once a global variable has been defined, it can be accessed and modified from any part of the program. Here's an example of how to use a global variable in a function

store = 23;

function yourNum() {
    console.log("Your number is " + store);



Your number is 23

In this example, the store variable is defined outside of the yourNum function, but it can still be accessed and used within the function. This allows you to store and manipulate data that is needed by multiple parts of the program, without having to pass the data between functions.


Creating global variables within the local scope

As stated to create global variables, we make use of the var keyword or simply we do not use any keyword before the variable. Using the var keyword, we can create global variables within a local scope that are available in the global scope. To illustrate this, we will create two variables using the let and var keyword, and see the result

    let another = 23;
    var newOne = 23;




ReferenceError: another is not defined

As the error states, the another variable is not defined because another is not a global variable and is accessed within the global scope which it wasn’t defined in. The let keyword defined the another variable within a local scope.

It's important to note that global variables are accessible to all scripts running on the page, including third-party scripts, which can lead to naming collisions and other issues. It is recommended to use global variables only when it is absolutely necessary and to avoid using them for data that is specific to a single function or block of code.


Using global variables across multiple files

In JavaScript, when using modules (CommonJS or ES6), variables and functions are only accessible within the module they are defined in. If you want to share a variable or function across multiple files, you can export it from the module it's defined in and import it in the modules that need to use it.

For example, if you have a variable called "globalVar" defined in a file called "global.js", you can export it like this:

// global.js
const globalVar = 'some value';
export { globalVar };

Then in another file, you can import it like this:

// otherFile.js
import { globalVar } from './global.js';
console.log(globalVar); // 'some value'

Alternatively, you can use global object window (in browser) or global (in node) to define global variable across files.

// global.js
window.globalVar = 'some value';

And in other file you can access it like this

// otherFile.js
console.log(window.globalVar); // 'some value'


Why we should avoid using too many global variables?

There are several reasons why it's generally considered best practice to avoid using global variables in JavaScript:

  1. Naming collisions: When multiple parts of your codebase use global variables with the same name, it can lead to naming collisions and unintended consequences. For example, if two different parts of your code both use a global variable called "x", then any changes made to "x" in one part of the code will affect the other part as well.
  2. Difficulty tracking down bugs: Because global variables can be accessed and modified from anywhere in the code, it can be difficult to track down bugs that are caused by unexpected changes to global state.
  3. Lack of modularity: When variables are global, they can be accessed and modified by any part of the codebase. This can make it difficult to create modular and self-contained code, where each module is responsible for its own state.
  4. Security issues: Having global variables can lead to security issues, as it could be possible for malicious code to access and modify them.
  5. Testing issues: When global variables are used, it can be difficult to test different parts of the code in isolation, since any changes made to global state will affect the entire codebase.



Global variables are an important feature of JavaScript that allows you to store and manipulate data that is needed by multiple parts of the program. However, it's important to use them carefully and to avoid using them for data that is specific to a single function or block of code. With a good understanding of global variables, you can create powerful and dynamic web pages that are easy to maintain and update.



Global variable - MDN Web Docs Glossary: Definitions of Web-related terms | MDN (
var - JavaScript | MDN (
Global object - MDN Web Docs Glossary: Definitions of Web-related terms | MDN (


Views: 74
Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment