Decoding OBJECTS in modern javascript - PART I;

cyruscodes

cyrusCodes

Posted on February 7, 2021

Decoding OBJECTS in modern javascript  - PART I;

Intro to objects;

Some of the most powerful data structures to work within javascript are arrays and objects. Both of these items have a lot in common and also can interact with each other. They both are used to store collections of data in javascript where arrays store lists of data while an object mostly stores properties of an item and its values.

an-array-of-1975394_1920.png
This article will highlight the inner workings of each of these data structures and how they can interact with each other in javascript. This article will also include several javascript code snippets and there are many ways of running them to view the results described in this article.

Working with javascript objects;

1. Object definition ;

The line of code below shows how to define an object in javascript language;

const user = {};//an empty object
Enter fullscreen mode Exit fullscreen mode

An object can also contain properties that appear as follows;

const user = { 
  name: "cyrusCodes",
  proficiency: "javascript",
};
Enter fullscreen mode Exit fullscreen mode

2. Accessing all javascript object values;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user);/*{ name: 'cyrusCodes', proficiency: 'javascript' }*/
Enter fullscreen mode Exit fullscreen mode

3. Accessing javascript object value;

There are two methods of accessing property values in a javascript object either by the use of dot notation (.) or the square bracket notation[].

- Using the dot (.) notation;

This methodology is the most recommended while coding in javascript language and it is utilized as follows;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user.name); /*cyrusCodes*/
Enter fullscreen mode Exit fullscreen mode

- Using the square bracket [] notation;

This methodology uses square brackets to access the property value of the javascript object as follows;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user["proficiency"]); /*javascript*/
Enter fullscreen mode Exit fullscreen mode

4. Reassigning a property value;

To achieve this in javascript, the use of assignment operator **(=) **is required as follows;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user.proficiency); /*javascript*/
user.proficiency = "front-end";
console.log(user.proficiency); /*front-end*/

Enter fullscreen mode Exit fullscreen mode

5. Adding a new property to a javascript object;

In this and coming examples, we will utilize the dot (.) notation in all examples related to javascript objects. In this case, the addition is achieved as follows;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user); /*{ name: 'cyrusCodes', proficiency: 'javascript' }*/
user.email = "cyruscodes@yahoo.com"; /*added property email*/
console.log(user); /*{ name: 'cyrusCodes',proficiency: 'javascript',email: 'cyruscodes@yahoo.com'}*/
Enter fullscreen mode Exit fullscreen mode

6. Removing / Deleting a property from a javascript object;

const user = {
  name: "cyrusCodes",
  proficiency: "javascript",
};
console.log(user);  /*{ name: 'cyrusCodes', proficiency: 'javascript' }*/
delete user.proficiency; /*deleting an object's property*/
console.log(user);/*{ name: 'cyrusCodes' }*/
Enter fullscreen mode Exit fullscreen mode

Thank you so much. Remember, there are tons of other operations one can work with when it comes to javascript objects. This post is meant to highlight the basics and an advanced series is on the way, but in the meantime, this concludes the first part of a blog series of posts that are meant to decode objects and arrays and how one can use a combination of the two. Have a look at this second part of the blog series that explains the workings of arrays in modern javascript. Stay tuned for more updates on this and much more.

Please ensure you follow me on Twitter for more detailed and comprehensive blog posts.

💖 💪 🙅 🚩
cyruscodes
cyrusCodes

Posted on February 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Decoding OBJECTS in modern javascript  - PART I;