In this lesson, we will learn about a new type of data structure called objects, with a focus on the following key points,
- What are objects and how are they used?
- Working with objects and key-value pairs in JavaScript.
For the Python version of this lesson, please click here.
Previously, we've studied arrays and other data types that can only store a single value of an element. Sometimes, you may encounter situations during your program when you need to refer to your data in pairs. In that case, objects
are used. They are a data structure in JavaScript that used a technique called hashing
to store the data in key-value
pairs.
Suppose you want to store information of students in a class, along with information identifiers. In this case, using variables or arrays is not recommended as you will have to create too many variables, and separating identifiers and their values from the array will be difficult. You will need something that stores and categorizes your data according to the identifiers. Objects are excellent for this purpose as they allow programmers to categorize data into key-value
pairs, that is, you can specify multiple values under a single identifier. This makes the problem of storing student information easier!