In JavaScript, objects are a fundamental data structure used to store and organize data. An object is a collection of key-value pairs, where each key is unique and associated with a value.
For example, consider an object representing a person:
1const person = {
2 name: 'John',
3 age: 30,
4 profession: 'Web Developer'
5};
In this example, the object person
has three properties: name
, age
, and profession
. Each property is associated with a specific value.
To access the values of an object, we can use either dot notation or bracket notation. Dot notation is commonly used when we know the property name beforehand. For example:
1console.log(person.name); // Output: John
2console.log(person.age); // Output: 30
3console.log(person.profession); // Output: Web Developer
Bracket notation, on the other hand, allows us to access property values dynamically using a variable or expression as the property name. For example:
1const property = 'name';
2console.log(person[property]); // Output: John
Objects are incredibly versatile and can be used to represent a wide range of entities and concepts. They are widely used in JavaScript for various purposes, including data modeling, creating complex data structures, and representing real-world entities.
Next, we will dive deeper into creating and modifying objects in JavaScript.
xxxxxxxxxx
const person = {
name: 'John',
age: 30,
profession: 'Web Developer'
};
console.log(person);