Ad space (banner)
🟨JavaScript Lessons
Lesson 56 / 69

Using static (Static Class Properties and Methods)

In this lesson you'll learn how to use a class's static properties and methods, so you can work with data shared across a whole class rather than per instance. This is for people searching "JavaScript static usage."

A property or method marked static belongs not to each individual instance but to the class itself. This is used when you want "a value shared across every instance," or "processing you can call without creating an instance." A key difference from an ordinary method is that you can call it directly as ClassName.methodName(), without creating an instance with new.

The sample code defines a shared counter, static count = 0, and a shared operation, static increment(), on the Counter class. Notice how Counter.count increases every time you call Counter.increment() — a class itself can hold state without creating any instances.

A common beginner stumbling block is confusing ordinary properties/methods with static ones. An instance (something created with new) can't access static members directly — you always have to go through the class name. If you're not aware of this distinction, it's easy to be confused when something ends up undefined for no apparent reason.

This is often used as a counter for counting how many instances have been created, or as a place to hold shared configuration values — an important part of object-oriented design. Get comfortable distinguishing between values that belong to each instance and values shared across the whole class.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)