Suggest an editImprove this articleRefine the answer for “What does the static keyword do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`static`** makes a property or method part of the class itself, not of its instances: static members are accessed through the class name (`ClassName.method()`) rather than through `this` on an object, and a regular instance has no access to them. **Key point:** static methods and properties are inherited with `extends`, and inside a static method `this` refers to the class itself, which is why static members are often used for factory methods, constants, and utility helpers like `Math.random()`.Shown above the full answer for quick recall.Answer (EN)Image## 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: | 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 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 | 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) ```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` 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:** > `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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.