Summary
In JavaScript, objects are written using curly braces:
const myObject = {
name: 'Simple Dev',
description: 'A site designed to teach coding',
age: 1,
active: true
};
Details
In JavaScript, you can store multiple values and give each value a name using an object. These names are called properties. Objects allows you to refer back to the value using the property name instead of a number (like with an array).
To access an object’s properties, use the following syntax:
myObject.propertyName
Here are some examples of this syntax:
console.log(myObject.name);
// Assign new value to property
myObject.description = 'New value';
You can also use square brackets to access and change property values. The property name must be in quotes inside the square brackets.
myObject['propertyName']
myObject['propertyName'] = 'New value';
Demo
Exercises
Try the following statements in the console:
const myObject = {
name: "Simple Dev",
description: "A site designed to teach coding",
age: 1,
active: true
}
myObject.name
myObject.name = "John Smith"
References
Object literals on MDN