JavaScript Static Variables

JavaScript Static Variables | When defining a static method or static attribute of a class, the word “Static” is used. A static method has the advantage of eliminating the requirement to create a class instance or object. It doesn’t have a range of values. It will have a single static value that is set up front.

The word “static” is comparable to the word “const.” These kinds of variables have a global presence and scope and are set at run time during startup. These static variables are available for use throughout the script. 

JavaScript’s static keyword is used to specify a class’s static methods or properties. We don’t need to make an instance or object of the class in order to call the static method.

JavaScript Static Variables Example

JavaScript static variables:- To declare a static variable in JavaScript, we can use the static keyword, similar to how a constant variable is defined with the const keyword. Such a type of variable functions as a global variable and is set at run time. The static variable is universal. Unlike the constant variable, the static variable’s value can be changed.

We construct a static variable in JavaScript for several reasons, including replication prevention, fixed-configuration support, and cache support. Also see:- Dynamic Variable Name in JavaScript

Example 1: In the example that follows, a static variable will be created and shown on the JavaScript console.

class Test {
    static staticVariable = "Hello";
    static staticMethod() {
        console.log("Static variable value: " + this.staticVariable);
        return "Static method is initiated.";
    }
}

console.log(Test.staticVariable);
console.log(Test.staticMethod());

As we can see from the program code above, a class contains a static method that, when called, calls a statement inside the class’s static method. Below is the result of the following:-

Output:-

Hello
Static variable value: Hello
Static method is initiated.

JavaScript Static Variables

We can even modify the JavaScript static variables. In the below program, a static variable is initialized in the Test class and later we modified it outside the class.

class Test {
    static staticVariable = "Hello";
}

console.log(Test.staticVariable);
Test.staticVariable = "Hi";
console.log(Test.staticVariable);

Output:-

Hello
Hi

Using JavaScript const Variable

Example 2:- Here is an example of how to use the JavaScript const keyword in practice:

const value = 2;
try {
    value = 4;
}
catch (e) {
    console.log(e.toString());
}

console.log(value);

The const variable is given a value in the code above, but when we attempt to assign a different value, an error is raised, indicating that the value designated as const cannot be modified. The result of the code above is thus:-

Output:-

TypeError: Assignment to constant variable.
2

We learned in this section that static and const variables serve different functions and serve different roles. A static variable may be used for a class property or method, whereas a const variable may be used to initialize a const value for an object, variable, or array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *