Skip to content
Related Articles

Related Articles

Difference Between Static and Const in JavaScript

Improve Article
Save Article
Like Article
  • Last Updated : 21 Oct, 2021

Static variable: A static variable in JavaScript is basically a property of the class which is not used on the object of the class but is used in the class itself. This static variable is stored into the data segment of the memory and its value is shared among all the objects/instances created in that particular class. To declare a variable/function as static we used the ‘static‘ keyword. In the case of a static variable, its value is set at the runtime itself and it is a global value that can be used by the instance of the class.

Example: In the following code, we have declared a static method in class z and printed it using the document .write() method.


    class z {  
        static staticMethod() {  
            return "Displaying geeks for "
              + "geeks using static method.";  


Constant: A constant variable in JavaScript is a variable that has a constant or a fixed value which remains the same ie. which does not change throughout the program. Any kind of modification in its value is not possible once it is declared. If the programmer tries to modify its value the compiler shows an error, this is because as soon as we have declared a variable as constant it tells the compiler that this is a fixed value and should be prevented from making any changes to it.

Example: Below is the implementation of const keyword in JavaScript. In the following code, we have declared a variable as const and using document.write() method, we have displayed its value.


    const value= 8; 



Difference between Static and Constant:

                                               Static                                                       Constant
The static methods are basically utility functions creating or making a copy of an object.The const variable is basically used for declaring a constant value that cannot be modified.
A static keyword is been used to declare a variable or a method as static.A const keyword is been used to assign a constant or a fixed value to a variable.
In JavaScript, the static keyword is used with methods and classes too.In JavaScript, the const keyword is used with arrays and objects too.
The value of a static variable can be modified.The value of the constant variable cannot be modified.
Static is a storage specifier.Const/Constant is a type qualifier.
Static can be assigned for reference types and set at run time.Constants are set at compile-time itself and assigned for value types only.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!