What does the static keyword do?
1. What a Static Property/Method Is
Normally, class methods and properties belong to instances (objects created with new):
class User {
constructor(name) {
this.name = name; // belongs to the instance
}
sayHi() { // also belongs to the instance
console.log(`Hi, ${this.name}`);
}
}
const u = new User("Alice");
u.sayHi(); // Hi, AliceBut if you add static:
class User {
static role = "User";
static createGuest() {
return new User("Guest");
}
constructor(name) {
this.name = name;
}
}
console.log(User.role); // "User"
console.log(User.createGuest()); // User { name: "Guest" }
const u = new User("Alice");
console.log(u.role); // undefined: an instance has no access to static membersStatic members are accessible only through the class itself (User),
not through its objects (u).
2. Why This Is Needed
Static properties and methods are used when data or functions:
- don't depend on a specific instance,
- are shared across the whole class.
Examples:
- factory methods (
User.createGuest()), - global constants (
Math.PI), - utility helpers (
Date.now()).
3. Examples of Built-in Static Methods
JS itself makes heavy use of static in its built-in classes:
| Class | Static Method |
|---|---|
Math | Math.random(), Math.max() |
Date | Date.now() |
Object | Object.keys(), Object.assign() |
Array | Array.isArray() |
For example, you don't create an instance of
Math, you just callMath.max(1, 5, 9), and that's a static method.
4. Inheriting Static Methods
If a class inherits from another (extends), then static methods are inherited too:
class Animal {
static category = "Animals";
static info() {
console.log("This is an animal");
}
}
class Dog extends Animal {}
console.log(Dog.category); // "Animals"
Dog.info(); // "This is an animal"In other words,
Doginherited its static members fromAnimal.
5. A Static Member Can Call Another Static Member
Inside a static method, you can access other static properties via this:
class Counter {
static count = 0;
static increment() {
this.count++;
}
static reset() {
this.count = 0;
}
}
Counter.increment();
Counter.increment();
console.log(Counter.count); // 2
Counter.reset();
console.log(Counter.count); // 0Here,
thisinsidestaticis the class itself, not an instance.
6. How static Differs From Regular Properties
| Characteristic | Regular Property | Static Property |
|---|---|---|
| Where it's stored | On each instance (this) | On the class itself (ClassName) |
| Accessible via an instance | Yes | No |
| Accessible via the class | No | Yes |
Inherited with extends | Yes | Yes |
this context inside the method | Instance | Class |
7. A Real-World Usage Example
Factory Method (Factory Pattern)
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
static createAdmin(name) {
return new User(name, "admin");
}
static createGuest() {
return new User("Guest", "guest");
}
}
const admin = User.createAdmin("Alice");
const guest = User.createGuest();
console.log(admin); // { name: "Alice", role: "admin" }
console.log(guest); // { name: "Guest", role: "guest" }Convenient when you need to create objects following certain templates.
8. You Can Combine It With Private Static Fields
ES2022 lets you declare private static properties with #:
class Config {
static #secret = "12345";
static getSecret() {
return this.#secret;
}
}
console.log(Config.getSecret()); // "12345"
console.log(Config.#secret); // SyntaxErrorSuch fields are visible only inside the class itself, even subclasses can't see them.
Summary
What static Does | Explanation |
|---|---|
| Binds a property/method to the class, not to objects | Accessed via ClassName, not via this |
| Often used for utilities, constants, factories | For example, Math.random() |
Inherited with extends | Subclasses get the parent's static members |
Inside static, this refers to the class itself | You can access other static properties |
Can be made private (static #field) | Visible only inside the class |
In short:
staticmakes a property or method part of the class, not part of its instances. It's an ideal tool for shared functions and constants.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.