JavaScript Abstraction

What is Abstraction in JavaScript?

In JavaScript, abstraction refers to the concept of hiding complex details and showing only an object's essential features or functionalities. Simply, it helps us reduce complexity and allows us to design efficiently and implement complex software systems. We can achieve abstraction with either abstract classes or with the help of interfaces

An abstraction is a way of hiding the implementation details and showing only the functionality to the users. In other words, it ignores the irrelevant details and shows only the required ones.

Points to Remember

  • We cannot create an instance of Abstract Class.
  • It reduces the duplication of code.

Why do we use Abstraction in JavaScript?

Abstraction in JavaScript, we have several important purposes to use it. Such as:

Simplification

With the use of abstraction, developers can simplify complex systems by hiding unnecessary details and exposing only the essential parts. It makes the code easier to understand and maintain.

Encapsulation

With the use of abstraction, we can encapsulate the implementation details of a particular function and we can allow the developers to focus on the functionality usage rather than how we can implement it. Because of this we can promote modularity and reduce coupling between the different parts of the codebase.

Reusability

When we abstract the common pattern and functionalities into reusable components it helps the developers to save time and effort by leveraging existing code rather than reusing the code each time.

Flexibility

With the help of abstraction, developers can create an interface that can be implemented differently under the hood. It provides flexibility for easier adaptation to changing requirements and environments without the need to modify a large portion of the program.

Maintainability

When we abstract away implementation details it will reduce the likelihood of bugs and it make changes or updates to the program without affecting the other parts of the system.

In JavaScript specifically, we can achieve abstraction with the help of functions, classes, modules, and libraries, among others. With the help of these tools, developers can organize and structure their code or program in a way that promotes clarity, maintainability, and also scalability.

How we can achieve abstraction in JavaScript?

In JavaScript, when we can create an abstraction that involves organizing your code in a way that hides the complex details and exposes only the essential features to other parts of the programs.

If you want to achieve abstraction in JavaScript you need to create an abstract class and also an interface even though JavaScript doesn't have native support for these types of concepts. Most developers often use prototypes, functions, and object-oriented patterns to enforce abstraction.

Example 1:

Output:

 
8

Example 2:

Let's take a program to check if we can create an instance of the Abstract class or not.

Output:

JavaScript Abstraction

Example 3

Let's take an example to achieve abstraction

Output:

JavaScript Abstraction

Example 4:

Output:

 
Area of rectangle1: 30
Area of rectangle2: 200