Arrays vs Objects in JavaScript

Arrays and Objects both are the data type that is used in storing and manipulating the user’s data. In this article we will know when we can use exactly which type of data storing method that can help us to achieve our goal in product development.

Arrays

Arrays used to store multiple values in a specific order. Every element in the array has an index. The counting of index starts from 0. Arrays are ideally used for lists, collections and sequences.

Now we will see some actions that we can operate on an array:

Creating an Array

let colors = ["red", "white", "yellow"]

In above example we have create an array named colors. In this array we have 3 elements and we can access them by their indexes i.e. 0,1,2. Lets see how to access the array elements.

Accessing the Array Elements

let colors = ["red", "white", "yellow"]
console.log(colors[0]) //"red"

Objects

In JavaScript objects are used to story key-value type data. It is used to story data with names or properties.

Now we will see some actions that we can operate on an array:

Creating an Object

let person = {
  name: "Satyam",
  age: 22,
  isStudent: true
};

Accessing Value in JavaScript

console.log(person.name);       // "Satyam"
console.log(person["age"]);     // 22

Modifying or Add Properties

person.age = 91;
person.country = "India";

Summary

In this blog we have seen how arrays and objects work in JavaScript. We have see to create array and object and how to modify or access their value.

Thank You 😊

3
Subscribe to my newsletter

Read articles from Satyam Vishwakarma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Satyam Vishwakarma
Satyam Vishwakarma