Skip to content

JavaScript Reference: Object

New Course Coming Soon:

Get Really Good at Git

All about the JavaScript Object properties and methods

This post documents all the Object built-in object properties and methods.

Any value that’s not of a primitive type (a string, a number, a boolean, a symbol, null or undefined) is an object. Even arrays or functions are, under the hoods, objects.

An object value can be generated using an object literal syntax:

const person = {}
typeof person //object

using the Object global function:

const person = Object()
typeof person //object

or using the Object constructor:

const person = new Object()
typeof person //object

Another syntax is to use Object.create():

const car = Object.create()

You can initialize the object with properties using this syntax:

const person = {
  age: 36,
  name: 'Flavio',
  speak: () => {
    //speak
  }
}

const person = Object({
  age: 36,
  name: 'Flavio',
  speak: () => {
    //speak
  }
})

const person = new Object({
  age: 36,
  name: 'Flavio',
  speak: () => {
    //speak
  }
})

All those ways are basically equivalent as they all give you access to the methods I’ll list below.

You can also initialize an object using the new keyword before a function with a capital letter. This function serves as a constructor for that object. In there, we can initialize the arguments we receive as parameters, to setup the initial state of the object:

function Car(brand, model) {
  this.brand = brand
  this.model = model
}

We initialize a new object using

const myCar = new Car('Ford', 'Fiesta')
myCar.brand //'Ford'
myCar.model //'Fiesta'

Objects have properties. Every property has a name and a value.

You might think an object is basically a map, or dictionary, data structure, and you would be correct.

The value of a property can be of any type, which means that it can even be an object, as objects can nest other objects.

When a property value is a function, we call it method.

Objects can inherit their properties from other objects, and we’ll see this in details when we’ll talk about inheritance.

Objects are always passed by reference.

If you assign a variable the same value of another, if it’s a primitive type like a number or a string, they are passed by value:

let age = 36
let myAge = age
myAge = 37
age //36
const car = {
  color: 'blue'
}
const anotherCar = car
anotherCar.color = 'yellow'
car.color //'yellow'

Built-in Object Properties

The Object object has 2 properties

Static methods

We divide methods in static methods, and instance methods. Static methods are called directly on Object. Instance methods are called on an object instance (an object).

Static methods are a great way to offer a namespace for functions that work in the same space. In this way we don’t have global functions around, but all are namespaced under the Object global object.

Instance methods

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: