Object Literals

Creating objects with literal syntax.

Examples

Creating an Object

Defining an object with multiple properties.

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};
console.log(person.firstName);

Empty Object

Creating an empty object and adding properties later.

const car = {};
car.make = "Toyota";
car.model = "Camry";
console.log(car);

Object Const

You can change properties of a const object, but you cannot reassign it.

const person = {name:"John", age:30};
person.age = 31; // This is allowed
// person = {name:"Bert"}; // This will cause an error

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.