March 30, 2025
A Deep Dive into JavaScript Classes: Static, Public, Private, and Protected
In this blog series, I’ve discussed the core concepts and features of classes in JavaScript. In this final post, we’ll explore an equally important topic: managing the accessibility of properties. According to MDN, The static keyword defines a static method or field for a class, or a static initialization block. Static properties cannot be directly accessed on instances of the class. Instead, they’re accessed on the class itself.MDN Static Methods class MyClass { static sayHi() { console.log("Hi from the class."); } } MyClass.sayHi(); // Hi from the class. const myInstance = new MyClass(); myInstance.sayHi(); // Error: myInstance.sayHi is not a function Enter fullscreen mode Exit