Mark As Completed Discussion

Objects

Objects allow for the storage of data in the form of key-value pairs. Keys (or names) are unique elements that are assigned a value. You can consider keys as indices or properties of an object. That is, the values stored in an object can be accessed using a key. Moreover, unlike arrays, objects can store a collection of elements (such as using an array) as a value for a single key.

Let's look at few examples of objects.

JAVASCRIPT
1var student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}

Visualizing this example, we see that the data is stored somewhat like the illustration below.

Objects

If we want to store multiple values for a single key, all the values are grouped in an array and assigned to the corresponding key.

JAVASCRIPT
1var students = {"ID": ["01", "02", "03", "04"], "name":["Patrick", "William", "Elizabeth", "John"], "age":[18, 19, 19, 20], "major":["Biology", "CS", "Literature", "Mathematics"]}

Storing multiple values within a single key stores the data in the form of tables within the program.

Objects

To access values in an object, keys are used as indices (using bracket notation) or through key name (as a property using dot notation).

JAVASCRIPT
1var student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3console.log(student["name"])
4console.log(student.age)