Abstraction in JavaScript — Explained in Simple Words (and How It Differs from Encapsulation)

Published: (April 22, 2026 at 09:11 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for Abstraction in JavaScript — Explained in Simple Words (and How It Differs from Encapsulation)

🧱 What is Abstraction?

In simple words, abstraction means:

Hiding the messy internal logic and showing only what is needed.

  • Abstraction is about what an object does.
  • Implementation is about how it does it.

You focus on the “what” and hide the “how.”

🚗 The “Car” Analogy

Think about driving a car. You only care about:

  • The steering wheel
  • The brake
  • The accelerator

You do not care about how the fuel moves into the engine or how the pistons fire. That is all hidden from you.

That is abstraction: the car gives you a simple interface (the pedals and wheel) so you don’t have to worry about the complex engine.

⚙️ Abstraction in JavaScript

JavaScript doesn’t have an abstract keyword like Java, so we achieve abstraction by designing our classes carefully:

  • Creating simple public methods.
  • Hiding the “helper” steps inside the class.

☕ Example: The Coffee Machine

class CoffeeMachine {
  // This is the only thing the user needs to know
  makeCoffee() {
    this.#boilWater();
    this.#addCoffeePowder();
    this.#pourIntoCup();
    console.log("Your coffee is ready! ☕");
  }

  // These are internal steps (hidden from the user)
  #boilWater() {
    console.log("Boiling water...");
  }

  #addCoffeePowder() {
    console.log("Adding coffee...");
  }

  #pourIntoCup() {
    console.log("Pouring into cup...");
  }
}

const myMachine = new CoffeeMachine();
myMachine.makeCoffee();

Why is this good?

The user only calls makeCoffee(). They don’t have to worry about the order of boiling water or adding powder. The complexity is abstracted away.

🔍 Abstraction vs Encapsulation

Many beginners mix these two up. Here’s an easy way to remember the difference:

  • Abstraction: Focuses on simplicity. It asks, “What should the user see?” (Hiding complexity).
  • Encapsulation: Focuses on safety. It asks, “How do we protect the data?” (Hiding state).

The Bank Analogy

  • Abstraction: You use the ATM screen to “Withdraw Money.” You don’t see the bank’s internal database logic.
  • Encapsulation: Your accountBalance is private. You can’t change it directly; you must use a deposit() method that validates the user first.

❌ Common Beginner Mistakes

  • Over‑abstracting: Don’t hide everything. If a user needs control, expose an appropriate method.
  • Mixing the two: Hiding a function to make code cleaner is Abstraction. Hiding a variable to prevent bugs is Encapsulation.
  • Thinking it’s only for big apps: Even a 50‑line script can benefit from abstraction to improve readability.

🎯 Interview‑Friendly Recap

If an interviewer asks, “What is Abstraction?”, you can say:

“Abstraction is hiding the internal implementation details of a system and showing only the essential features to the user. It reduces complexity and makes the code easier to maintain.”

✅ Final Thoughts

Abstraction isn’t about being “fancy.” It’s about being kind to your future self (and other developers). By hiding the messy parts of your code, you make your objects easier to use and harder to break.

Think of your classes as a black box—give them a simple input, get a simple output, and keep the “magic” hidden inside.

0 views
Back to Blog

Related posts

Read more »

Building a Markdown editor (Markflow)

I’ve been working with Markdown editors both as a user. At some point I wanted to better understand how they actually behave under the hood, especially when doc...