DEV Community

Cover image for What Does `static` Mean in JavaScript? Explained in Simple Words
SAURAV KUMAR
SAURAV KUMAR

Posted on

What Does `static` Mean in JavaScript? Explained in Simple Words

If you are learning Object-Oriented Programming (OOP) in JavaScript, one keyword that can feel a little confusing in the beginning is: static.

You may see code like this:

class User {
  static count = 0;
}
Enter fullscreen mode Exit fullscreen mode

and then wonder:

  • Why is count written with static?
  • Why do we access it with the class name?
  • Why is it not inside each object?

When I first started learning this, the biggest confusion was very simple:
What belongs to the object, and what belongs to the class itself?

Once that becomes clear, static becomes much easier.

In this post, let’s understand static in very simple words.


🧱 What does static mean?

In JavaScript, static means:

This property, method, or block belongs to the class itself, not to each object created from that class.

That is the main idea. So whenever you see static, ask yourself one question:
Should this belong to one object, or to the whole class?

If it belongs to the whole class, static is usually the right choice.


🚗 Instance vs. Static: The Basic Idea

Let’s take a simple User example.

Every user has their own:

  • name
  • email
  • password

These should be instance members, because each object has its own unique value.

But something like:

  • the total number of users created

should be static, because that value is shared by the entire class.

Let’s see that in code:

class User {
  static count = 0; // Static property

  constructor(name) {
    this.name = name; // Instance property
    User.count++;
  }
}

const u1 = new User("Saurav");
const u2 = new User("Rahul");

console.log(u1.name);    // Saurav
console.log(u2.name);    // Rahul
console.log(User.count); // 2
Enter fullscreen mode Exit fullscreen mode

What is happening here?

  • this.name is an instance property. Each object gets its own name.
  • User.count is a static property. All objects share one count.

So name belongs to the object, but count belongs to the class. That is the core idea of static.


🧠 Easy Memory Trick

Use instance when the data is personal to one object.
Use static when the data is shared by the whole class.

Instance Examples: name, email, employeeId, password
Static Examples: companyName, totalUsers, schoolName, count


⚙️ Static Method vs. Instance Method

static is not only for properties. We can also use it with methods.

1. Static Method

A static method belongs to the class.

class MathUtils {
  static add(a, b) {
    return a + b;
  }
}

console.log(MathUtils.add(5, 3)); // 8
Enter fullscreen mode Exit fullscreen mode

Here, we did not create any object. That is because add() does not depend on one specific object’s data. It only works on the inputs given to it. So this is a perfect use case for a static method.

2. Instance Method

Now compare that with this:

class User {
  constructor(name) {
    this.name = name;
  }

  sayHello() {
    console.log(`Hello, I am ${this.name}`);
  }
}

const user1 = new User("Saurav");
user1.sayHello();
Enter fullscreen mode Exit fullscreen mode

Here, sayHello() uses this.name. That means it depends on one specific object’s data. So it should be an instance method, not static.


❓ Can a static method access instance data?

This is a very common beginner confusion. Let’s say we write this:

class User {
  constructor(name) {
    this.name = name;
  }

  static showName() {
    console.log(this.name); // ❌ This will not work!
  }
}
Enter fullscreen mode Exit fullscreen mode

This does not work the way beginners usually expect.

Why? Because inside a static method, this refers to the class, not to one specific object. A static method does not automatically know which object you mean.

The Correct Way:

If you need instance data in a static method, you have to pass the object yourself:

class User {
  constructor(name) {
    this.name = name;
  }

  static showUserName(user) {
    console.log(user.name); // ✅ This works!
  }
}

const user1 = new User("Saurav");
User.showUserName(user1); // Saurav
Enter fullscreen mode Exit fullscreen mode

The Very Important this Rule

  • In an instance method: this = current object
  • In a static method: this = the class itself

📦 What is a Static Block?

In modern JavaScript, classes can also have a static block. A static block is a block of code that runs once, when the class is evaluated.

class Config {
  static appName;
  static version;

  static {
    console.log("Static block runs once");
    Config.appName = "Interview Prep App";
    Config.version = "1.0.0";
  }
}

console.log(Config.appName); // Interview Prep App
Enter fullscreen mode Exit fullscreen mode

It is incredibly useful for one-time setup, initializing static data, or preparing class-level configuration.

Static Block vs Constructor

  • Constructor: Runs every time you create a new object. (Object setup)
  • Static Block: Runs only once for the class. (Class setup)

🏗️ Real-World Use Cases for static

Here are some practical cases where static makes perfect sense:

  1. Shared counters: Tracking how many objects were created (User.count).
  2. Utility methods: Helper functions that just take inputs and return outputs (MathUtils.add(a, b)).
  3. Factory methods: Special methods used to create predefined objects (e.g., User.createAdmin("Saurav")).
  4. Shared configuration: App-wide settings (AppConfig.version).

(Note: If you use TypeScript, the concept is exactly the same!)


❌ Common Beginner Mistakes

  1. Accessing static members through an object: You cannot do user1.count. You must do User.count.
  2. Making everything static: If a method depends on an individual object's data, keep it as an instance method.
  3. Confusing utility methods: If a method formats data (like formatCurrency(amount)), it's usually static. If a method logs a user in using their specific email, it should be an instance method.

🎯 Interview-Friendly Answer

If an interviewer asks: "What is static in JavaScript?"

You can answer like this:

"static means a property, method, or block belongs to the class itself instead of individual objects created from that class. Static members are accessed using the class name, while instance members are accessed using objects. Static is useful for shared data, helper methods, factory methods, and one-time class setup."


✅ Final Takeaway

The easiest way to remember static is:

  • Instance = belongs to one object.
  • Static = belongs to the class as a whole.

If you remember only that, many OOP examples will start making much more sense!


👋 About Me

Hi, I’m Saurav Kumar.

I enjoy learning, building, and writing about web development in simple words, especially topics that are useful for beginners and developers preparing for interviews.

Right now, I’m also focusing on improving my understanding of core concepts like JavaScript, OOP, system design, and software engineering fundamentals.

Let's connect!

If you found this helpful, share it with a friend learning JavaScript — it might help them too.

Until next time, keep coding and keep learning 🚀

Top comments (0)