Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Static Properties in JavaScript
Static properties in JavaScript belong to the class itself rather than its instances. They can be accessed directly on the class without creating objects, making them useful for storing class-level data like constants or shared values.
Syntax
// Function constructor approach
function ClassName(params) {
// instance properties
}
ClassName.staticProperty = value;
// ES6 class approach
class ClassName {
static staticProperty = value;
}
Example: Static Properties with Function Constructor
Static Properties Example
Example: Static Properties with ES6 Classes
ES6 Static Properties
Comparison: Static vs Instance Properties
| Property Type | Access Method | Shared Across Instances | Memory Usage |
|---|---|---|---|
| Static | ClassName.property | Yes | Single copy |
| Instance | instance.property | No | Copy per instance |
Common Use Cases
Static Properties Use Cases
Key Points
- Static properties belong to the class, not instances
- Accessed using
ClassName.propertysyntax - Useful for constants, counters, and shared data
- Memory efficient - only one copy exists
- Cannot be accessed through instance objects
Conclusion
Static properties provide a way to store class-level data that is shared across all instances. They are perfect for constants, utility values, and maintaining class-wide state like counters.
Advertisements
