JavaScript Reference: Object
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
length
always equal to1
prototype
this points to the Object prototype object: the object that all other objects inherit from. Check the prototypal inheritance post for more.
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.
Object.assign()
*ES2015
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
*ES2017
Object.freeze()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
*ES2015
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.seal()
Object.setPrototypeOf()
*ES2015
Object.values()
Instance methods
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025