Difference between Array and Array of Objects in JavaScript

In this article, we will see the differences between Array and Array of Objects in JavaScript.

  1. Array: An Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, …, etc. The array can store data types like Integer, Float, String, and Boolean all the primitive data types can be stored in an array.

Example:

var myArr = [1, 2, 3, 4, 5];

// Iterating through loop

for (var i = 0; i < myArr.length; i++) {

console.log(myArr[i]);

}

// Pop an element from array

myArr.pop();

console.log(“after using pop()” + myArr);

Output

1

2

3

4

5

“after using pop() 1,2,3,4,5”

Array and Array of the object respectively.

  • Arrays are best to use when the elements are numbers.
  • The data inside an array is known as Elements.
  • The elements can be manipulated using [].
  • The elements can be popped out of an array using the pop() function.
  • Iterating through an array is possible using For loop, For..in, For..of, and ForEach().
  • objects are best to use when the elements strings (text).
  • The data inside objects are known as Properties that consist of a key and a value.
  • The properties can be manipulated using both .DOT notation and [].
  • The keys or properties can be deleted by using the delete keyword.
  • Iterating through an array of objects is possible using For..in, For..of, and ForEach().

Leave a comment

Your email address will not be published. Required fields are marked *