Last Updated : 05 Aug, 2025
In JavaScript, Abstraction can be defined as the concept of hiding the inner complex workings of an object and exposing only the essential features to the user.
In JavaScript, abstraction is often achieved through functions, classes, and modules that encapsulate behaviour and expose only the necessary parts to the outside world.
JavaScript
class P{
constructor(name, age) {
this.name = name;
this.age = age;
}
getD() {
return `${this.name} is ${this.age} years old.`;
}
}
const p1 = new P("Anuj", 30);
console.log(p1.getD());
Anuj is 30 years old.
In this example
The JavaScript does not provide built-in support for implementing the abstraction like the other programming language gives. However we can implement abstraction in JavaScript using functions, objects, closures, and classes.
Using FunctionsFunctions are one of the most simple ways to introduce abstraction in JavaScript. They allow you to wrap complex logic into a reusable block of code, exposing only the function name and parameters.
JavaScript
function a(radius) {
return Math.PI * radius * radius;
}
console.log(a(5));
Using Objects and Methods
Classes and objects provide a more structured way to achieve abstraction by bundling related properties and methods into a single unit.
JavaScript
const car = {
brand: "Toyota",
start: function() {
console.log("Car started");
}
};
car.start();
Using Closures
Closures help in abstraction by restricting access to certain variables, making them private.
JavaScript
function Count() {
let c1 = 0;
return {
inc: function() {
c1++;
console.log(c1);
}
};
}
const c2 = Count();
c2.inc();
c2.inc();
Using Classes and Encapsulation
ES6 classes help implement abstraction by using constructor functions and private fields (using closures or symbols).
JavaScript
class B{
#balance;
constructor(B1) {
this.#balance = B1;
}
deposit(amount) {
this.#balance += amount;
console.log(`Deposited: $${amount}`);
}
getB() {
return this.#balance;
}
}
const a1 = new B(1000);
a1.deposit(500);
console.log(a1.getB());
Deposited: $500 1500Use Cases of Abstraction
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4