Objects
An object is a collection of key-value pairs. Unlike a primitive value, an object can hold many values at once, and each value can be of any type, including another object or a function. JavaScript objects are similar to dictionaries or hash maps in other languages. We use them to keep related data together and to read it back by name.
const person = {
name: "Alice",
age: 30,
greet: function () {
console.log(`Hello, my name is ${this.name}`);
},
};
console.log(person.name);
person.greet();
In this chapter, you will learn about JavaScript objects, including plain objects with their properties and methods, arrays as ordered collections, and the built-in Date object for working with dates and times.
Learning Outcomes
- Create and manipulate plain objects, arrays, and their properties or elements using literal syntax, dot/bracket notation, and destructuring
- Use JavaScript’s built-in objects (Date and Math) to work with dates, times, and mathematical operations
- Use Map and Set for key-value and unique-value collections, and decide when to use them over plain objects and arrays