Skip to main content

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):

javascript
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, Alice

But if you add static:

javascript
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 members

Static 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:

ClassStatic Method
MathMath.random(), Math.max()
DateDate.now()
ObjectObject.keys(), Object.assign()
ArrayArray.isArray()

For example, you don't create an instance of Math, you just call Math.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:

javascript
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, Dog inherited its static members from Animal.


5. A Static Member Can Call Another Static Member

Inside a static method, you can access other static properties via this:

javascript
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); // 0

Here, this inside static is the class itself, not an instance.


6. How static Differs From Regular Properties

CharacteristicRegular PropertyStatic Property
Where it's storedOn each instance (this)On the class itself (ClassName)
Accessible via an instanceYesNo
Accessible via the classNoYes
Inherited with extendsYesYes
this context inside the methodInstanceClass

7. A Real-World Usage Example

Factory Method (Factory Pattern)

javascript
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 #:

javascript
class Config { static #secret = "12345"; static getSecret() { return this.#secret; } } console.log(Config.getSecret()); // "12345" console.log(Config.#secret); // SyntaxError

Such fields are visible only inside the class itself, even subclasses can't see them.


Summary

What static DoesExplanation
Binds a property/method to the class, not to objectsAccessed via ClassName, not via this
Often used for utilities, constants, factoriesFor example, Math.random()
Inherited with extendsSubclasses get the parent's static members
Inside static, this refers to the class itselfYou can access other static properties
Can be made private (static #field)Visible only inside the class

In short:

static makes 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.