Loading...

Static vs Const in JavaScript-Complete Guide

Static vs Const in JavaScript-Complete Guide

Hey readers! In this article, we will be talking about some differences between static versus const in JavaScript.

Introduction

JavaScript is one of the widely used languages when it comes to building web applications as it permits developers to encase HTML and CSS code in it to make web apps interactive. It facilitates the interaction of users with the web application. JavaScript has lots of libraries

Use cases: 

  • Building web server and its interactive functions
  • Animations and graphics, adding special effects to web components
  • Validating forms and exception errors
  • Adding behavior and functionalities to web pages

Understanding static vs const

Static variable:

In JavaScript, a static variable is a class attribute that is not utilized on the class’s object but is used within the class itself. This static variable is stored in the memory’s data segment, and its value is shared by all objects/instances generated in that class. The ‘static‘ keyword was used to declare a variable or function as static. A static variable’s value is determined at runtime, and it is a global value that can be utilized by any instance of the class.

Const variable:

In JavaScript, a constant variable is a variable with a fixed or constant value that does not change throughout the program. It is not possible to change its value once it has been declared. When a programmer tries to change the value of a variable, the compiler throws an error. This is because declaring a variable as a constant tells the compiler that it has a fixed value and should not be changed.

Difference between Static vs Const

Static    Constant
Static methods are essentially utility functions for constructing or copying objects. The const variable is mostly used to declare a non-modifiable constant value.
To declare a variable or procedure as static, use the static keyword. To assign a constant or fixed value to a variable, use the const keyword.
The static keyword is also used with methods and classes in JavaScript. The const keyword is also used with arrays and objects in JavaScript.
A static variable’s value can be changed. The constant variable’s value cannot be changed.
A storage specifier is static. A type qualifier is Const/Constant.
For reference types, static can be assigned and set during runtime. Constants are defined at build time and are only assigned to value types.

Conclusion

This was about the difference between static versus const in JavaScript. 

Check out our course: Learn JavaScript by Tetris game!!

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far