Javascript Objects for beginners.

·

2 min read

Introduction

I started this blog because I wanted to chronicle my journey as a developer by writing about every new feature, language, framework and tech that I learnt about. Today I will be discussing Javascript Objects. Let us begin:

Objects in Javascript are data structures that allow us to store and label data. Objects are made up of properties, these properties are divided into two parts, the name, which we call a key and the value of the property. Objects are created by assigning them to a variable and using curly braces {} that define the start and end of a function. Inside the curly braces is where we have the properties, we also use commas to separate the properties. A typical Javascript Object would look like this:

const userInfo = {
     name: 'Adam Williams',
     DOB: 8/14/2000,
     gender: Male
};

Adding a new property to an object

Adding a new property to an object is extremely simple all we have to do is call the variable that the object is referenced in, the specific key you want to add and the value you want to add inside it. An example:

userInfo.nationality = 'Canadian';

Updating an existing property

The syntax for updating an existing property is essentially the same. An example:

userInfo.gender = 'female';

This would change the gender from male to female in the console.

Nested Objects

Nested Objects are objects that exist inside other objects. An example:

const student = {
    firstName: 'James',
    lastName: 'Williams',
    age: 13,
    grades:{
      midTerm: 90,
      quizzes: 66,
      finals: 84,
      finalAvg: 80
    }
};

Conclusion

Objects are used to store data with a key identifier thus allowing us to optimize our code much easier. I hope this quick article clears up whatever issue you may have had with javascript objects.