BeginnerCoder
intermediate#javascript#oop

Understanding JavaScript Object-Oriented Programming

This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.

Learn how to use object-oriented programming concepts in JavaScript to create reusable and maintainable code.

Introduction to JavaScript Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to organize and structure code.

Step 1: Creating a Class

In JavaScript, you can create a class using the class keyword.

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

Step 2: Creating an Object

You can create an object from a class using the new keyword.

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}
let person = new Person('John Doe', 30);
console.log(person);

Step 3: Inheritance

You can use inheritance to create a new class that inherits properties and methods from an existing class.

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}
class Employee extends Person {
    constructor(name, age, salary) {
        super(name, age);
        this.salary = salary;
    }
}
let employee = new Employee('John Doe', 30, 50000);
console.log(employee);

By following these steps, you can learn how to use object-oriented programming concepts in JavaScript.

Keep going with javascript

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.