Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 5 - Basic Data Structures

5.4 Objects

Objects are collections of key-value pairs, declared using curly braces {}.

For example, think about your exam mark sheet where all your subjects are listed along with the marks. This mark sheet can be represented as an object where the subject names are keys and the marks are values.

In an object, each key is a unique identifier, and each value is the data associated with that key.

You can declare an object as follows:

let person = {
    name: 'Alex',
    age: 25,
    city: 'London'
};

To access a specific value from an object, follow this general syntax myObject.key. For example:

let person = {
    name: 'Alex',
    age: 25,
    city: 'London'
};
console.log(person.city);

Try to pass a different key inside the console.log() and observe the output.

Similarly, you can use the key to edit specific values in an object. For example:

let person = {
    name: 'Alex',
    age: 25,
    city: 'London'
};
person.city = 'New York';
console.log(person.city);

Here, we changed the value of city to New York.