Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 2 - Basic JavaScript Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
Lesson 10 - User Input
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
.