JavaScript ES6 Objects

Last modified: April 24, 2018

Objects allow you to define your own data types in JavaScript. Similar to a dictionary or hash map, objects are a collection of key/value pairs which can be modified throughout the object's life cycle.

let person = {
  name: "Sam",
  age: 33,
  printName: function() {
    console.log("Hello, my name is " + + ".");
//returns 'Sam'

//returns 33

//logs 'Hello, my name is Sam.'

Creating Objects

You can use the {} syntax to define an object like this:

let myObject = {}

You can also use the Object constructor to create an object like this:

let myObject = new Object();

You can also define an object via a constructor function:

function Person(name, age){ = name
  this.age = age

let jeff = new Person("Jeff", 25)
//returns "Jeff"

//returns 25

Constructor functions optionally accept parameters. Notice how our constructor function for Person takes two parameters (name, age). We set properties for these arguments via this. When we initialize or create a new instance of Person, the properties name and age get set based on the supplied arguments.

We can also use the Object.create() method to create new objects from a prototype. This allows us to create prototypes without a constructor function:

let person = {
  name: "Sam",
  age: 50

let sam = Object.create(person)
//returns 'Sam'

//returns 50

let fred = Object.create(person) = "Fred"
//returns 'Fred'

Notice how we override the default name Sam for our second object.

Accessing Object Properties

You can access object properties via dot notation:

let myObj = {prop1:"hello", prop2:"goodbye"}

//returns 'hello'

You can also access properties like this:

//returns 'hello'

Changing Object Properties

You can change an object property's value the same way you reassign a variable:

let person = {name:"Steve", age:35} = "Sam"
//returns 'Sam'

person["name"] = 'Ted'
//returns 'Ted'

Adding Object Properties

You can dynamically create new properties as well:

let person = {name:"Steve", age:35}

person.lastName = "Smith"

//logs {name: 'Steve', age: 35, lastName: 'Smith'}

Deleting Object Properties

You can delete object properties via the delete operator:

let person = {name:"Steve", age:35}


"name" in person
//returns false

Comparing Objects

In JavaScript, each object you create points to a different memory address. This means objects with identical properties are still not considered equal:

let myObj = {a:1, b:2}
let myObj2 = {a:1, b:2}
let objRef = myObj2

myObj == myObj2
//returns false

myObj === myObj2
//returns false

objRef == myObj2
//returns true

objRef === myObj2
//returns true

Cloning Objects

You can clone an object with the Object.assign() method:

let myObj = {a:1, b:2}
let copy = Object.assign({}, myObj)


//logs {a: 1, b: 2}

ES6 Shortcuts

ES6 introduces a few shortcuts for working with objects:

let name = "John"
let person = { name }


//logs {name: 'John'}


Objects allow you to build and extend custom data types. Objects make it easy to create and dynamically change properties and are fundamental to JavaScript programming.

You might also like: