JavaScript Objects: Properties and Methods
Understand JavaScript objects, properties, methods, dot notation and bracket notation with beginner-friendly examples.
Describe a value with properties
An object groups related information using named properties. This example describes one book:
const book = {
title: "Small Web Projects",
pages: 180,
available: true
};
Read and update properties
Use dot notation when the property name is known in your code. Bracket notation is useful when the property name is stored in a variable.
console.log(book.title);
book.available = false;
const field = "pages";
console.log(book[field]);
Add a method
A method is a function stored as an object property. It can use this to refer to the object that owns it.
const user = {
name: "Sam",
greet() {
return `Hello, ${this.name}`;
}
};
Objects group values by name, while arrays keep values in an ordered list.