Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript get Function
Next article icon

Abstraction in JavaScript

Last Updated : 08 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • Hiding Complexity: Implementation is hidden, it shows only the necessary details.
  • Modularity: Code is organized in a reusable form, which improves maintainability and readability.
  • Security: Important data cannot be directly accessed, they are hidden.
  • Reusability: The code can be reused across different applications.

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());  

Output
Anuj is 30 years old. 

In this example

  • The class P encapsulates both data (name, age) and behaviour (getD method).
  • The getD() method provides a simple, reusable way to retrieve a formatted description.
  • Creating an instance (p1) and calling getD() is straightforward, with the underlying logic hidden.
  • You can create multiple instances of P, each maintaining its own data while reusing the same logic.

Implementing Abstraction in JavaScript

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 Functions

Functions 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));  

Output
78.53981633974483 

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(); 

Output
Car started 

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();  

Output
1 2 

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());  

Output
Deposited: $500 1500 

Use Cases of Abstraction

  • API Development: Abstraction can be used to create the APIs where the only necessary functionality is exposed and internal logic is hidden.
  • Database Operations: Database interactions are abstracted using Object-Relational Mapping (ORM) frameworks, so developers work with high-level models instead of raw SQL queries.
  • Middleware in Web Servers: Web servers use middleware functions to abstract authentication, logging, and error handling.
  • Security and Encryption: Sensitive operations like password hashing and encryption are abstracted into reusable functions, preventing direct manipulation of security mechanisms.

Benefits of Abstraction in JavaScript

  • Better Code Quality: Your code will be simpler and easier to read.
  • No Repeated Code: You’ll store shared logic in one place instead of copying it everywhere.
  • Easier Updates: Changing one piece of logic won’t break other parts of the application.
  • Team-Friendly: Developers can work on different parts without needing to know every detail of the codebase.

Next Article
JavaScript get Function

A

amanv09
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-oop

Similar Reads

  • Encapsulation in JavaScript
    Encapsulation is a fundamental concept in object-oriented programming that refers to the practice of hiding the internal details of an object and exposing only the necessary information to the outside world. Encapsulation can be achieved using two techniques: Table of Content Using ClosuresUsing Cla
    3 min read
  • Interesting Facts About JavaScript
    JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It is an interpreted, high-level programming language that follows ECMAScript. It powers interactive websites and is packed with amazing features that make it special and powerful. Here are some inter
    5 min read
  • Introduction to JavaScript
    JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library. JavaScript is a single-threaded language that executes one task at
    8 min read
  • JavaScript Function() Constructor
    The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
    2 min read
  • JavaScript get Function
    JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript's getter function. The get
    3 min read
  • Are functions objects in javascript?
    Yes, Functions are considered first-class objects, which means they have the same capabilities as other objects. The following are examples that demonstrates functions behaving as objects: Can be assigned to variable [GFGTABS] JavaScript // Assign a function to a variable const x = function() { retu
    1 min read
  • Creating objects in JavaScript
    An object in JavaScript is a collection of key-value pairs, where keys are strings (properties) and values can be any data type. Objects can be created using object literals, constructors, or classes. Properties are defined with key-value pairs, and methods are functions defined within the object, e
    5 min read
  • Explain about IIFEs in JavaScript
    In this article, you will learn about JavaScript immediately invoked function expressions (IIFE). A JavaScript immediately Invoked Function Expression is a function defined as an expression and executed immediately after creation. The following shows the syntax of defining an immediately invoked fun
    3 min read
  • JavaScript Function Examples
    A function in JavaScript is a set of statements that perform a specific task. It takes inputs, and performs computation, and produces output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different
    3 min read
  • JavaScript Basics
    JavaScript is a versatile, lightweight scripting language widely used in web development. It can be utilized for both client-side and server-side development, making it essential for modern web applications. Known as the scripting language for web pages, JavaScript supports variables, data types, op
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences