Introduction to Objects {} in Javascript

hash-obj.png Objects represent one of Javascript data types, It is used to store collections and more complex entities in key-value pairs. Objects can be created using the Object() constructor or the object initializer/Literal Syntax.

In other programming languages like python, objects are called dictionaries, using the dictionary, as an illustration, word has meanings, the words represent the key and the meaning represents the value.

bag: "flexible container with opening at the top, used for carrying things."

In the scenario above, the bag is the property key and the definition is the object value.

First, let's see an example of an object created with object literal syntax

let object = {
    'key1': 'value1',
    'key2': 'value2',
    'keyn': 'valuen',
  };

Objects can also be created using the new keyword.

let object = new Object();

Using the Object constructor method to create an object

  let student = new Object();
  student.firstName = "Francis";
  student.lastName = "Emmanuel";
  student.age = 60;

Using the object literal notation to create an object

      let student2 = {
      firstName: "Francis",
      lastName: "Emmanuel",
      age: 06,
}

Accessing the properties of an object using the dot notation

     console.log(student.firstName);
     console.log(student.lastName);
     console.log(student.age);

Accessing the properties of an object using the bracket notation

      console.log(student2['firstName']);
      console.log(student2['lastName']);
      console.log(student2['age']);

Adding a new property to an object using the dot notation

The syntax below checks if the height key is present and adds the height key to the student object with the value of 180 if the height key does not exist.

      student.height = 180;
      student.middleName = "Doe";

Adding a new property to an object using the bracket notation

The bracket notation behaves similarly to the dot notation.

      student2['height'] = 180;
      student2['middleName'] = "Doe";

Change the property value of an object using the dot notation

If the key name is present in the object, you can change the value of that key using the syntax below.

      student.firstName = "Jane";
      student.lastName = "Cyprus";

Change the property value of an object using the bracket notation

This also behaves similarly to the dot notation.

      student2['firstName'] = "Jane";
      student2['lastName'] = "Thomas";

Objects can be elements of an array

Objects can be an element of an array and can be accessed using array methods.

        let listOfObjects = [
          {
              'key1': 'value1',
              'key2': 'value2',
              'key3': 'value3',
          },
          {
              'key4': 'value4',
              'key5': 'value5',
              'key6': 'value6',
          },
          {
              'key7': 'value7',
              'key8': 'value8',
              'key9': 'value9',
          },
      ]
console.log(listOfObjects[0]); //gives us the first Object in the array

Objects can also be a property value

In the example below, an object is used as the property value for the prefects key and we can access the nested property values using the dot or bracket notation.

        let student = {                
          firstName:"Jane",           
          lastName:"Doe",
          age:18,
          prefects: {
              firstName:"Francis",
              lastName:"Emmanuel",
          }
      }; 
      console.log(student.prefects); //gives the prefects object
      console.log(student.prefects.firstName); // Francis

Conclusion

Objects are useful for solving algorithm challenges because they have a Big O notation of constant time (O(1)).

This is just an introduction to Objects in javascript and is just to set the foundation of the legion of things that can be done with this Javascript data structure, Other data structures worth looking into are Arrays, Sets, and hashmaps.