What Are Access Modifiers In JavaScript ?
Last Updated :
24 Apr, 2025
Access modifiers in JavaScript play a significant role in controlling the visibility and accessibility of class members. Although JavaScript is not traditionally an object-oriented programming (OOP) language, the introduction of classes and access modifiers in ECMAScript 6 (ES6) allowed developers to implement OOP principles into their applications.
Access modifiers in JavaScript are a crucial aspect of the language that allows developers to control the visibility and availability of variables and functions in their code. These modifiers determine whether certain elements can be accessed or modified from different parts of the program. JavaScript provides three main access modifiers: private, public, and protected, each of which serves a specific purpose in terms of encapsulating and hiding information.
Types of Access Modifiers
- Public (default)
- Private
- Protected
Purpose of Access Modifiers
- The main purpose of access modifiers is to control and restrict access to members of a class. This encapsulation ensures data integrity, and security and helps adhere to the principle of least privilege.
- Access modifiers also improve code maintainability and collaboration between team members. By explicitly defining who can access and modify class members, you minimize unexpected side effects and improve code clarity.
Public Access Modifier
Members marked as public can be accessed from anywhere. Public members are the default in JavaScript classes. They are accessible in and out of the classroom. For example, methods marked as public can be called from anywhere in your code.
Example:
JavaScript
class Person {
constructor(name) {
this.name = name; // Public member
}
introduce() {
console.log(`Hello, my name is ${this.name}.`);
}
}
const person = new Person('Alice');
person.introduce(); // Accessing a public method
console.log(person.name); // Accessing a public property
OutputHello, my name is Alice.
Alice
Explanation: In the public access modifier example, a Person class is defined with a public member called name. Public members are directly accessible outside the class. The introduce method allows for the public property name to be accessed and displayed. An instance of the Person class is created, and both public methods and properties are accessed, demonstrating the concept of public access.
Private Access Modifier
Members marked as private are only accessible within the class itself. Private members are marked with the # symbol. These members are only accessible from within the class itself. They are invisible to external code. Private members provide encapsulation and ensure that the internals of a class remain hidden.
Example:
JavaScript
Javascript
class Person {
#name; // Private member
constructor(name) {
this.#name = name;
}
#sayHello() {
console.log(`Hello, my name is ${this.#name}.`);
}
introduce() {
// Accessing a private method
this.#sayHello();
}
}
const person = new Person('Bob');
// Accessing a public method that
// accesses a private method
person.introduce();
// Error: Private member is not accessible
console.log(person.#name);