Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is encapsulation in JavaScript

Understanding Encapsulation

When we start learning about programming, we often come across the term 'encapsulation'. So, what is encapsulation, and why is it important? In simple terms, encapsulation is a way of wrapping up the data and the functions that manipulate that data into a single unit. It's like a capsule (hence the name 'encapsulation') that holds everything together.

Breaking Down Encapsulation

To better understand encapsulation, let's think of it as a smartphone. The smartphone is a perfect encapsulation example. It contains various components like the battery, the processor, the camera, etc. (these are like the data in our program). It also has various functions that it can perform like calling, texting, clicking pictures (these are like the functions in our program). All of these components and functionalities are nicely packaged or 'encapsulated' inside a sleek case.

In programming terms, encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them. This is also known as data hiding.

Encapsulation in JavaScript

JavaScript is a bit unique when it comes to encapsulation. Unlike some other programming languages, JavaScript doesn't have a specific keyword for declaring a class (until ES6 came along), but we can achieve encapsulation using functions and some special features of JavaScript.

Let's take an example. Consider we are creating a simple counter. We need a variable to hold the count and a function to increase the count. Here's how we can do this without encapsulation:

let count = 0;

function increaseCount() {

This works, but there's a problem. The count variable is exposed. Anyone can change the count without using the increaseCount function.

With encapsulation, we protect the count variable. Here's how we can do this:

function createCounter() {
    let count = 0;

    return {
        increaseCount: function() {

const counter = createCounter();

Now, the count variable is hidden, and it can only be changed using the increaseCount function. This is encapsulation in action.

Why is Encapsulation Important?

There are several reasons why encapsulation is important:

Data Hiding: As we saw in the counter example, encapsulation allows us to hide the data and expose only the required functionalities. This prevents the data from being modified accidentally or intentionally, which can lead to bugs.

Code Organization: Encapsulation helps us to organize our code better. Each object is responsible for its own data and how that data is manipulated. This makes the code easier to understand and maintain.

Flexibility and Reusability: Encapsulation allows us to make changes in one part of the code without affecting other parts. If we need to change how the count is increased in the counter example, we only need to change the increaseCount function. Everything else remains the same.


So, that's encapsulation in JavaScript. Just like a capsule holds everything together, encapsulation in JavaScript is about keeping related data and functions together, neatly packaged into a single unit. It's an incredibly powerful concept that helps us write code that is robust, organized, and easy to maintain. Remember, good encapsulation is like a well-made smartphone. Everything is there, in its place, working seamlessly together, and you, the user, don't have to worry about the complexities inside. Happy coding!