In JavaScript, an object is a collection of properties, and a property is an association between a name (or key) and a value. Objects can be used to store various types of data and are a fundamental part of the language.
Syntax of an Object
let person = { firstName: "John", lastName: "Doe", age: 30, profession: "Engineer" };
In the above example, person
is an object with four properties: firstName
, lastName
, age
, and profession
.
Accessing Object Properties
You can access properties of an object using dot notation or bracket notation:
console.log(person.firstName); // Output: John console.log(person["age"]); // Output: 30
Modifying Object Properties
You can modify the properties of an object like this:
person.age = 31; person["profession"] = "Software Developer"; console.log(person.age); // Output: 31 console.log(person.profession); // Output: Software Developer
Adding New Properties
You can add new properties to an object:
person.country = "USA"; console.log(person.country); // Output: USA