Skip to content
    geeksforgeeks
    • Interview Prep
      • DSA
      • Interview Corner
      • Aptitude & Reasoning
      • Practice Coding Problems
      • All Courses
    • Tutorials
      • Python
      • Java
      • ML & Data Science
      • Programming Languages
      • Web Development
      • CS Subjects
      • DevOps
      • Software and Tools
      • School Learning
    • Tracks
      • Languages
        • Python
        • C
        • C++
        • Java
        • Advanced Java
        • SQL
        • JavaScript
        • C#
      • Interview Preparation
        • GfG 160
        • GfG 360
        • System Design
        • Core Subjects
        • Interview Questions
        • Interview Puzzles
        • Aptitude and Reasoning
        • Product Management
        • Computer Organisation and Architecture
      • Data Science
        • Python
        • Data Analytics
        • Complete Data Science
        • Gen AI
        • Agentic AI
      • Dev Skills
        • Full-Stack Web Dev
        • DevOps
        • Software Testing
        • CyberSecurity
        • NextJS
        • Git
      • Tools
        • Computer Fundamentals
        • AI Tools
        • MS Excel & Google Sheets
        • MS Word & Google Docs
      • Maths
        • Maths For Computer Science
        • Engineering Mathematics
        • School Maths
    • JS Tutorial
    • Web Tutorial
    • A to Z Guide
    • Projects
    • OOP
    • DOM
    • Set
    • Map
    • Math
    • Number
    • Boolean
    • Exercise
    • Interview Questions
    Open In App

    Object Oriented Programming in JavaScript

    Last Updated : 11 Sep, 2025
    Comments
    Improve
    Suggest changes
    151 Likes
    Like
    Report

    Object Oriented Programming (OOP) is a style of programming that uses classes and objects to model real-world things like data and behavior. A class is a blueprint that defines the properties and methods an object can have, while an object is a specific instance created from that class.

    Why OOP is Needed:

    Before OOP, when the code size grows and multiple people work on a project, there problems arise

    • Changing in one team's code causing other codes to break. Hence difficult to maintain.
    • Large number of parameters during function calls.
    • Difficult to divide and maintain code across teams.
    • Limited Code Reusability
    • Not scalable as the code is not modular.

    Object Oriented Programming (OOP) solves these problems by combining data and the functions that operate on it into a single unit called an object. This approach has following main features

    • Encapsulation ensures that one team can change data representation and algorithms without causing other team's code change.
    • Inheritance ensures code reuse.
    • Polymorphism allows objects to behave differently using the same interface.
    See More


    Types-of-OOPS-2

    Objects

    In JavaScript, an object is a collection of data (properties) and actions (methods) stored as key–value pairs.

    • Properties hold values like strings, numbers, or even other objects.
    • Methods are functions inside the object that define what it can do.

    Objects let you group related data and functionality together in one place.

    Classes

    In JavaScript, a class is a blueprint for creating objects with specific properties and methods. A class itself doesn’t hold values, it describes what an object should have and do. You create actual objects from a class using the new keyword.

    Example:

    C++
    // Class definition
    class Car {
      constructor(brand, model) {
        this.brand = brand; // property
        this.model = model; // property
      }
    
      // method
      showDetails() {
        console.log(`This car is a ${this.brand} ${this.model}.`);
      }
    }
    
    // Creating objects from the class
    const car1 = new Car("Toyota", "Corolla");
    const car2 = new Car("Honda", "Civic");
    
    // Using the objects
    car1.showDetails(); // This car is a Toyota Corolla.
    car2.showDetails(); // This car is a Honda Civic.
    

    Output:

    This car is a Toyota Corolla.
    This car is a Honda Civic.

    Read:

    • JavaScript Classes
    • Objects in JavaScript

    Abstraction

    Abstraction is one of the key features of object-oriented programming in JavaScript. It means showing only the essential information and hiding unnecessary details from the user. Data abstraction refers to exposing only what is necessary to interact with the object while keeping the background details or internal logic hidden.

    Learn here: Abstraction in JavaScript

    Encapsulation

    Encapsulation is defined as wrapping up data and information under a single unit. In Object-Oriented Programming, encapsulation is defined as binding together the data and the functions that manipulate them together in a class.

    Learn here: Encapsulation in JavaScript

    Inheritance

    The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.

    • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
    • Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass.

    Learn here: JavaScript Inheritance

    Polymorphism

    The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of an entity to behave different in different scenarios. person at the same time can have different characteristics.

    Learn here: Polymorphism in JavaScript

    These were all the essential OOP concepts in JavaScript, covering their complete mechanism and implementation. As OOP is an important part of JavaScript programming as it helps in building structured, reusable, and maintainable code. By using classes, objects, inheritance, polymorphism, encapsulation, and abstraction, developers can write cleaner programs that model real-world scenarios effectively while keeping the code easy to update and scale.

    Create Quiz

    Introduction to OOP (Object Oriented Programming)

    S

    Sumit Ghosh
    Improve

    S

    Sumit Ghosh
    Improve
    Article Tags :
    • JavaScript
    • Web Technologies
    • javascript-oop

    Explore

      JavaScript Basics

      Introduction to JavaScript

      4 min read

      Variables and Datatypes in JavaScript

      6 min read

      JavaScript Operators

      5 min read

      Control Statements in JavaScript

      4 min read

      Array & String

      JavaScript Arrays

      7 min read

      JavaScript Array Methods

      7 min read

      JavaScript Strings

      5 min read

      JavaScript String Methods

      9 min read

      Function & Object

      Functions in JavaScript

      5 min read

      JavaScript Function Expression

      3 min read

      Function Overloading in JavaScript

      4 min read

      Objects in JavaScript

      4 min read

      JavaScript Object Constructors

      4 min read

      OOP

      Object Oriented Programming in JavaScript

      3 min read

      Classes and Objects in JavaScript

      4 min read

      What Are Access Modifiers In JavaScript ?

      5 min read

      JavaScript Constructor Method

      7 min read

      Asynchronous JavaScript

      Asynchronous JavaScript

      2 min read

      JavaScript Callbacks

      4 min read

      JavaScript Promise

      4 min read

      Event Loop in JavaScript

      4 min read

      Async and Await in JavaScript

      2 min read

      Exception Handling

      Javascript Error and Exceptional Handling

      6 min read

      JavaScript Errors Throw and Try to Catch

      2 min read

      How to create custom errors in JavaScript ?

      2 min read

      JavaScript TypeError - Invalid Array.prototype.sort argument

      1 min read

      DOM

      HTML DOM (Document Object Model)

      8 min read

      How to select DOM Elements in JavaScript ?

      3 min read

      JavaScript Custom Events

      4 min read

      JavaScript addEventListener() with Examples

      9 min read

      Advanced Topics

      Closure in JavaScript

      4 min read

      JavaScript Hoisting

      6 min read

      Scope of Variables in JavaScript

      3 min read

      JavaScript Higher Order Functions

      7 min read

      Debugging in JavaScript

      4 min read
    top_of_element && top_of_screen < bottom_of_element) || (bottom_of_screen > articleRecommendedTop && top_of_screen < articleRecommendedBottom) || (top_of_screen > articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
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
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • Software and Tools
  • Courses
  • ML and Data Science
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Interview Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
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