DEV Community

Barrios Freddy
Barrios Freddy

Posted on • Edited on

7 2

Three ways to create an object in JavaScript

In JavaScript, there are three ways to create your own objects. Taking into account that almost everything in JS It’s an object.

Object literals

The simplest way to create an object in JS it’s through curly brackets { }.

Define and create a single object in one statement

const person = {
    name : 'Freddy',
    sayHello() {
        return `Hi ${this.name}`
    }
};

console.log(person.sayHello()) // Hi Freddy
Enter fullscreen mode Exit fullscreen mode

New operator

Using the new operator is the same thing as creating objects literally. It’s recommended to use object literals, instead of this, for simplicity and execution speed.

const person = new Object()
person.name = 'Freddy'
person.sayHello = ()  => {
   return `Hi ${this.name}`
}

console.log(person.sayHello()) // Hi Freddy
Enter fullscreen mode Exit fullscreen mode

Also, you can create an object through a constructor function. In this case, the new operator returns an instance of the function, if the function has not an explicit return statement it’ll “this “

function Person (name) {
    this.name = name
    this.sayHello = function() {
        return `Hi ${this.name}`
    }
}
const person = new Person('Freddy')
console.log(person.sayHello()) // Hi Freddy
Enter fullscreen mode Exit fullscreen mode

Object.create() method

In order to define and create a new object through the create
method, we have to use the prototype from another one.

const person = Object.create(Object.prototype)
person.name = 'Freddy'
person.sayHello = function sayHello() {
     return `Hi ${this.name}`
}


console.log(person.sayHello()) // Hi Freddy
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (4)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
barriosdfreddy profile image
Barrios Freddy

You're right, thank you!

Collapse
 
alimrandev profile image
Abdullah Al Imran • Edited

class Person {
constructor(name){
this.name = name
};
sayHello(){
return Hi ${this.name}
}
}
const person = new Person('Jon Doe');

console.log(person.sayHello()); //HI Jon Doe

You can also use the constructor function to create an object in es6.

Collapse
 
barriosdfreddy profile image
Barrios Freddy

Yeah... that's it, thank you

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more