How to Use Static Classes in JavaScript?

Estimated read time 1 min read

JavaScript does not have built-in support for static classes, but you can create a similar effect by using ES6 classes and declaring properties and methods as static.

Here’s an example of how to create a static class in JavaScript:

class MathHelper {
  static pi = 3.14;

  static circumference(radius) {
    return 2 * MathHelper.pi * radius;

  static area(radius) {
    return MathHelper.pi * radius * radius;

console.log(MathHelper.circumference(10)); // 62.8
console.log(MathHelper.area(10)); // 314

In this example, the MathHelper class is a static class with two static methods, circumference and area. These methods can be called directly on the class, without the need to create an instance of the class.

It’s important to note that in JavaScript, you cannot use a static class to store state that is unique to each instance. All properties and methods declared in a static class are shared by all instances of the class.

Static classes are useful when you want to provide utility functions or constant values that do not depend on instance state and can be used by any code in your application.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply