DEV Community

Cover image for How to Create Objects in JavaScript
Oluwadamisi Samuel Praise
Oluwadamisi Samuel Praise

Posted on

How to Create Objects in JavaScript

Creating objects in JavaScript is fundamental to programming in the language. Objects are collections of key-value pairs, where each key is a string (also called a property), and each value can be anything, including other objects, arrays, or functions. This article will cover several ways to create objects in JavaScript, providing clear examples for each method.

1. Object Literals

The simplest way to create an object in JavaScript is by using an object literal. This method is straightforward and concise.

const person = {
    name: "John Doe",
    age: 30,
    greet: function() {
        console.log("Hello, my name is " + this.name);
    }
};

// Accessing properties and methods
console.log(person.name); // Output: John Doe
person.greet(); // Output: Hello, my name is John Doe
Enter fullscreen mode Exit fullscreen mode

In the example above, person is an object with properties name and age, and a method greet.

2. The new Object() Syntax

Another way to create an object is by using the new Object() syntax. This approach is less common but serves the same purpose.

const car = new Object();
car.make = "Toyota";
car.model = "Camry";
car.year = 2020;

console.log(car.make); // Output: Toyota
Enter fullscreen mode Exit fullscreen mode

This method initializes an empty object and then assigns properties to it.

3. Using a Constructor Function

For creating multiple objects of the same type, you can use a constructor function. Constructor functions are a template for creating objects.

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.greet = function() {
        console.log("Hello, my name is " + this.name);
    };
}

const john = new Person("John Doe", 30);
const jane = new Person("Jane Doe", 28);

john.greet(); // Output: Hello, my name is John Doe
jane.greet(); // Output: Hello, my name is Jane Doe
Enter fullscreen mode Exit fullscreen mode

Here, Person is a constructor function that initializes new Person objects with specified name and age properties.

4. The Object.create() Method

The Object.create() method creates a new object with a specified prototype object and properties.

const personProto = {
    greet: function() {
        console.log("Hello, my name is " + this.name);
    }
};

const person1 = Object.create(personProto);
person1.name = "John Doe";
person1.age = 30;

person1.greet(); // Output: Hello, my name is John Doe
Enter fullscreen mode Exit fullscreen mode

In this example, person1 is created with personProto as its prototype, inheriting the greet method.

5. Classes (ES6)

ES6 introduced classes, which provide a clearer and more concise way to create objects and handle inheritance.

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

    greet() {
        console.log("Hello, my name is " + this.name);
    }
}

const john = new Person("John Doe", 30);
const jane = new Person("Jane Doe", 28);

john.greet(); // Output: Hello, my name is John Doe
jane.greet(); // Output: Hello, my name is Jane Doe
Enter fullscreen mode Exit fullscreen mode

Classes make the code more readable and resemble other object-oriented languages like Java or C#.

6. Using Factory Functions

Factory functions are another pattern for creating objects. These functions return new objects without using new.

function createPerson(name, age) {
    return {
        name: name,
        age: age,
        greet: function() {
            console.log("Hello, my name is " + this.name);
        }
    };
}

const john = createPerson("John Doe", 30);
const jane = createPerson("Jane Doe", 28);

john.greet(); // Output: Hello, my name is John Doe
jane.greet(); // Output: Hello, my name is Jane Doe
Enter fullscreen mode Exit fullscreen mode

Factory functions are flexible and can include additional logic to initialize the objects.

Summary

In JavaScript, there are multiple ways to create objects, each suited to different scenarios:

  • Object Literals: Best for simple and single objects.
  • new Object(): A less common, verbose alternative.
  • Constructor Functions: Useful for creating many similar objects.
  • Object.create(): Ideal for prototypal inheritance.
  • Classes (ES6): Provides a clear, concise syntax for creating objects and handling inheritance.
  • Factory Functions: Flexible functions that return new objects. Each method has its own advantages and use cases. Understanding these methods allows you to choose the best one for your particular programming needs.

Connect with me on Linkedin and Twitter if you found this helpful

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

You missed Object.fromEntries

Collapse
 
oluwadamisisamuel1 profile image
Oluwadamisi Samuel Praise

Thank you for the feedbank Randy!
I'll get right to it.