Back to JavaScript
2026-02-196 min read

static (JavaScript)

Learn static (JavaScript) step by step with clear examples and exercises.

Title: Mastering Static in JavaScript - A full guide

Why This Matters

Static methods and properties are a fundamental aspect of object-oriented programming (OOP) in JavaScript, providing a way to create shared functionality across instances of a class. Understanding static members is crucial for writing cleaner, more efficient code, especially when dealing with utility functions or configuration settings that don't depend on individual object states. This knowledge can help you tackle real-world bugs, impress interviewers, and make your code easier to maintain.

Static methods and properties allow for better encapsulation of related functionality within a class, improving code organization and reducing the risk of naming conflicts with global functions or variables. They also provide a means to create utility functions that can be easily accessed without creating an instance of the class.

Prerequisites

Before diving into static methods and properties, ensure you have a solid understanding of the following concepts:

  1. JavaScript basics: variables, functions, objects, and classes
  2. Understanding OOP principles in JavaScript, including inheritance and prototypes
  3. Familiarity with ES6 syntax and features like arrow functions, template literals, and destructuring assignments
  4. Basic understanding of the call stack and event loop
  5. Comprehension of common JavaScript design patterns such as module pattern, reveal module pattern, and IIFE (Immediately Invoked Function Expression)
  6. Understanding the difference between instance methods and static methods
  7. Knowledge of class fields introduced in ES2017

Core Concept

Static Methods

Static methods belong to a class rather than individual instances, meaning they can be called without creating an object or using this. They are defined using the static keyword before the method name. Static methods are often used for utility functions that don't require access to instance-specific data.

class MyClass {
static myStaticMethod() {
console.log("This is a static method.");
}
}

// Calling the static method without creating an instance
MyClass.myStaticMethod(); // Output: "This is a static method."

Static Properties

Similar to static methods, static properties are class-level variables that can be accessed directly on the class itself. They are defined using the static keyword before the property name and an assignment operator. Unlike instance properties, they cannot be modified once set.

class MyClass {
static myStaticProperty = "This is a static property.";
}

// Accessing the static property without creating an instance
console.log(MyClass.myStaticProperty); // Output: "This is a static property."

Static Methods and Prototypes

Note that that static methods are not part of the prototype chain, so they cannot be accessed through instances directly. Instead, you can call them on the class itself or use them within instance methods if needed.

Class-level vs Global Scope

While static members provide a way to group related functionality within classes, it's essential to understand that they are not the same as global functions or variables. Static members offer better encapsulation and organization compared to global scope, making it easier to manage related functionality and avoid naming conflicts with other global functions/variables.

Class Fields

Introduced in ES2017, class fields provide a more concise syntax for defining static properties and instance properties within classes. They are defined using the static keyword for static properties or simply assigning values to properties for instance properties.

class MyClass {
// Static property using class fields
static myStaticProperty = "This is a static property.";

// Instance property using class fields
myInstanceProperty = "This is an instance property.";
}

// Accessing the static property without creating an instance
console.log(MyClass.myStaticProperty); // Output: "This is a static property."

Worked Example

Let's create a MathUtils class with static methods for calculating the factorial of a number and finding the greatest common divisor (GCD) between two numbers using class fields.

class MathUtils {
// Static property to store the factorial cache
static factorialCache = {};

// Static method to calculate the factorial of a number
static factorial(n) {
if (this.factorialCache[n]) return this.factorialCache[n];
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
this.factorialCache[n] = result;
return result;
}

// Static method to find the greatest common divisor (GCD) between two numbers
static gcd(a, b) {
if (!b) return a;
return this.gcd(b, a % b);
}
}

// Using the MathUtils class to calculate factorials and find GCDs
console.log(MathUtils.factorial(5)); // Output: 120
console.log(MathUtils.gcd(48, 18)); // Output: 12

Common Mistakes

  1. Forgetting to use the static keyword for static methods and properties: This will result in an error when trying to access them on the class or creating instances with those methods/properties.
  1. Trying to access static members using this inside the method/property: Since static members belong to the class, not individual instances, this refers to the global object (window in a browser environment).
  1. Modifying static properties directly: As mentioned earlier, static properties cannot be modified once set. Attempting to do so will result in an error.
  1. Calling static methods on instance objects: Since static methods are not part of the prototype chain, they cannot be accessed through instances directly. Instead, you should call them on the class itself or pass instances as arguments if needed.
  1. Using this inside a static method to access instance properties: Since static methods don't have access to this, you cannot directly access instance properties using this. However, you can pass instances as arguments or use static properties if needed.
  1. Creating private static methods: While there is no direct way to create private static methods, you can achieve this by using closures or IIFEs within your static methods.

Practice Questions

  1. Write a Shape class with a static method to calculate the total area of all shapes created from that class, using instance properties for storing the area of each shape.
  1. Create a Person class with static methods to generate random names and ages, and an instance method to display personal information.
  1. Implement a Logger class with a static method to log messages at different levels (e.g., debug, info, warning, error).
  1. Write a Database class that uses static properties for storing connection details and a static method to establish a connection to the database.
  1. Create a Counter class with a static property to store the total count of created instances and a static method to increment the count each time a new instance is created.

FAQ

  1. Why use static methods and properties instead of global functions or variables? Static members provide better encapsulation and organization within classes, improving code organization and reducing the risk of naming conflicts with other global functions/variables.
  1. Can I access instance properties inside a static method? No, since static methods don't have access to this, they cannot directly access instance properties. However, you can pass instances as arguments or use static properties if needed.
  1. Are static members inherited by child classes in JavaScript? Static members are not part of the prototype chain and are not inherited by child classes. They must be defined separately for each class.
  1. Can I create private static methods in JavaScript? While there is no direct way to create private static methods, you can achieve this by using closures or IIFEs within your static methods.
  1. Is it possible to call a static method on an instance of the class? No, since static methods are not part of the prototype chain, they cannot be accessed through instances directly. However, you can pass instances as arguments if needed.
  1. What happens when I define both a static property and an instance property with the same name? If you define both a static property and an instance property with the same name, the instance property will take precedence, meaning it will be accessible on instances but not on the class itself. To access the static property, you can use MyClass.staticPropertyName.
static (JavaScript) | JavaScript | XQA Learn