In various programming languages, both static and const variables serve important roles. This section will delve into the differences between static and const variables as utilized in multiple programming languages.
What is a Static variable in JavaScript
A static variable refers to a property of a class that is utilized at the class level rather than at the instance level. This variable resides in the data segment of memory, allowing all instances created from the class to share the same value. To declare a static variable, the static keyword is employed. This keyword can also be applied to create static values, static methods, and can be associated with classes, operators, and properties, serving as utility functions for applications or websites. The value assigned to a static variable is determined at runtime, functioning as a sort of global value accessible to instances of the designated class.
What is a Const variable in JavaScript
A const variable is defined as a variable that holds a constant value, which does not alter during the execution of the program. One of the defining characteristics of a const variable is that its value cannot be altered or modified at any point in the program. This is due to the fact that once a variable is declared as const, the compiler is notified that the value is immutable and should be safeguarded against any changes by the programmer. Therefore, if the programmer attempts to change a const value, an error message is generated indicating that the const value is unchangeable. To declare a const variable, the 'const' keyword is utilized followed by the assigned value.
Static vs. Const in JavaScript
The subsequent points will help clarify the distinctions between the two:
| Static | Const |
|---|---|
| The static keyword is used for defining static properties and methods in a javascript class program. | The const keyword is used for defining constant value for a variable. |
| The static keyword can be accessed on the class definition only. In order to access the static keyword for non-static methods, one needs to invoke them using the class name. However, for calling a static method within another static method, we can make use of this keyword. | A const value can be accessed globally or locally, but a global constant can never be window object properties. |
| The static methods are the utility functions that are used for creating or cloning the objects. | The const variable is used for declaring a constant or fixed value whose value cannot be changed. |
| JavaScript static is labeled by a keyword known as the 'static' keyword. | JavaScript const is labeled by a keyword known as the 'const' keyword, where we declare a const variable and initialize it with a constant value. |
| JavaScript static can be used with classes and methods also. | JavaScript const can be used with objects and arrays also. |
| The value for a static variable can be reassigned. | The value for a const variable cannot be reassigned. However, we can re-declare the const variable in different block scope as it is allowed. |
Above are some difference points that will make us understand the working of both JavaScript keywords. Apart from these theoretical difference descriptions, let's have a look over an example of both through which we can understand the use and working of the static and const variable.
Using JavaScript Static
Here is a hands-on example demonstrating the use of the static keyword in a JavaScript class:
<html>
<head>
<title> JavaScript Static</title>
</head>
<body>
<script>
class A {
static staticMethod() {
return "Calling Static method.";
}
}
document.write(A.staticMethod());
</script>
</body>
</html>
In the program code presented above, a static method is defined within a class, and upon its invocation, it executes the statements contained within that static method of the class. The resulting output from this execution is displayed below:
Using JavaScript const
Presented here is a hands-on example demonstrating the use of the const keyword in JavaScript:
<html>
<head>
<title> JavaScript Static</title>
</head>
<body>
<script>
const value= 8;
try {
value= 10;
}
catch (e) {
document.write(e);
} //will display a TypeError
document.write(value);
</script>
</body>
</html>
In the code provided, the const variable is initialized with a specific value. However, when an attempt is made to assign a different value to it, an error occurs indicating that a variable declared as const cannot be altered. Therefore, the result of executing the above code is:
In this segment, we have learned that static and const variables serve distinct functions and are intended for various applications. A static variable can be utilized within a class method or property, while a const variable is employed to set a constant value for an array, a variable, or an object.