Home » Array in JavaScript

Array in JavaScript

Array in JavaScript

What is an array?

An array in JavaScript is a data structure that allows you to store and organize a collection of values. These values can be of various data types, including numbers, strings, objects, or even other arrays.

javascripty-array.png

Why do we need an array?

Like the Wishlist in Amazon is just an array, contact lists is an array, list of movies on Netflix is an array of objects.

Here is real real-life example where an array is used to store the information about the player in the leaderboard.

Here, the player information is stored in the array in the form of objects. Let’s see:

need-of-array.png
let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
]
JavaScript

Features of an Array in JavaScript

In JavaScript, arrays behave differently from Java, so lets discuss some features in JS which are different from JAVA

  1. Dynamic Sizing: In JS arrays are dynamically sized, meaning, we can add or remove elements from an array without specifying a fixed size. Now lets say you want to add 100 playerInfo to “array”.
let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
       .
       .
       .
   { name: 'suraj', score: 10 }
  ];
JavaScript

In the above example, we can add as many elements as we want in the array

Heterogeneous Elements

JavaScript can hold elements of different data types in the same array. Below is an example for it

let array = [1, "string", true, {key:"value"}];
JavaScript

In the above array, we have stored a number, string, boolean, and object in the same array.

Okay Now let's discuss some basic operations related to arrays:

  • Declaration of an array
  • Access an element of an array
  • Length of an array
  • Print the array

Declare an Array in JavaScript?

Arrays are defined using square brackets [ ] and can hold multiple elements, which are separated by commas.

Here’s an example of how to create an array in JavaScript:

let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
]
JavaScript

In this example, we have created an array called “playerInfo” that contains the player Information that is shown on “LeaderBoard”.

Suppose you want to access a player’s information. How will you do that?

You can access individual elements in an array using their index, starting from 0 for the first element. For example:

let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
]

// Accessing single element using index
console.log(playerInfo[2]) // output : { name: 'Rahul', score: 80 }
JavaScript

Suppose you want to find the length of your leaderboard, which stores information about player profiles. How would you do that?

To find the length of an array in JavaScript, you can use the length property of the array. Here’s how you can do it:

let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
]
console.log(playerInfo.length) // Output:6
JavaScript

Suppose you wish to print the information of a player in the leaderboard. How would you accomplish that?

To print the contents of an array in JavaScript, you can use For Loop.

Using a Loop

You can iterate through the array and print each element one by one.


let playerInfo = [
  { name: 'Priyanshu', score: 100 },
  { name: 'Krishna', score: 90 },
  { name: 'Rahul', score: 80 },
  { name: 'Abhinav', score: 80 },
  { name: 'Akhil', score: 70 },
  { name: 'Anuj', score: 60 }
];
// Accessing elements using a loop
for (let i=0;i<playerInfo.length;i++){
console.log(playerInfo[i])
}
// Output:
//{ name: 'Priyanshu', score: 100 }
//{ name: 'Krishna', score: 90 }
//{ name: 'Rahul', score: 80 }
//{ name: 'Abhinav', score: 80 }
//{ name: 'Akhil', score: 70 }
//{ name: 'Anuj', score: 60 }
JavaScript

Conclusion

JavaScript arrays are a fundamental part of managing and operating on collections of data. They provide a flexible way to store, access, and manipulate multiple values under a single name. With the array methods and properties available in JavaScript, you can perform a wide range of operations efficiently, such as adding, removing, finding, and sorting elements. Understanding arrays and how to work with them is crucial for any JavaScript developer, as they are widely used in various applications, from simple scripts to complex web applications.

Frequently Asked Questions

Q1. What is an array in JavaScript?

Ans: An array in JavaScript is a data structure used to store multiple values in a single variable. It allows for the organization and management of collections of data, such as lists of names, items, or even complex objects.


Q2. Why do we need arrays?

Ans: Arrays simplify the process of storing and manipulating groups of related data. Instead of creating numerous variables for each item, you can use an array to store them under one name and access them using an index. This is especially useful for handling dynamic data where the amount of data isn’t known in advance.


Q3. How do you declare an array in JavaScript?

Ans: You declare an array using square brackets [] and separating the elements with commas. For example, let fruits = ["Apple", "Banana", "Cherry"];.


Q4. How can you access an element in an array?

Ans: You access an array element by referring to its index number, starting with zero. For instance, fruits[0] would access the first element, “Apple”, in the fruits array.


Q5. How do you find the length of an array?

Ans: Use the length property of the array. For example, fruits.length would give the number of elements in the fruits array.