JavaScript Arrays


Arrays are sophisticated variables that allow us to store multiple values or groups of values under a single variable name. JavaScript arrays can hold any valid value, including strings, numbers, objects, functions, and even other arrays, allowing for the creation of more complex data structures like an array of objects or an array of arrays.

Syntax

But what if you need to store the state or city names of a country in variables, and this time there are more than three? It is difficult and tedious to save each of them in a distinct variable. Using so many variables at the same time and keeping track of them all will be a challenging effort. And here is where the array comes into play. Arrays solve this problem by storing multiple values or groups of values in an ordered structure.


Creating an Array

In JavaScript, the simplest way to create an array is to enclose a comma-separated list of values in square brackets. The Array() constructor can also be used to create an array, as shown in the following syntax.

Example

Preview

Accessing the Elements of an Array

The square bracket syntax can be used to access array items by index. An index is an integer that represents the location of an element in an array. Array indexes start at zero. This implies that the first item in an array is kept at index 0 rather than index 1, the second item at index 1, and so on. Array indices begin at 0 and increase to the number of items minus one. As a result, an array of five components would have indices ranging from 0 to 4.

Example

Preview

Getting the Length of an Array

The length property of an array returns the total number of elements present in the array. The length of an array is always bigger than the index of any of its elements.

Example

Preview

Looping Through Array Elements

The for loop may be used to access each element of an array in sequential order. The for-of loop, introduced in ECMAScript 6, is a simpler way to iterate through array elements. You do not need to initialise or maintain track of the loop counter variable in this loop (i).

Example

Preview

Adding New Elements to an Array

  1. Use the push() function to add a new element to the end of an array.
  2. Similarly, use the unshift() function to insert a new element at the beginning of an array.
  3. You can also use the push() and unshift() methods to add multiple elements at once.

Example

Preview

Removing Elements from an Array

The pop() function may be used to remove the final entry from an array. The value that was popped out is returned by this procedure. Similarly, the shift() function may be used to delete the first entry from an array. This function also returns the shifted out value.

Example

Preview

FAQs

JavaScript arrays are ordered, indexed collections of values. They allow you to store multiple values of different types, such as numbers, strings, objects, or even other arrays, in a single variable. Arrays in JavaScript are dynamic, meaning they can grow or shrink in size as needed. They provide various methods and properties for manipulating and accessing their elements, such as adding or removing elements, iterating over values, sorting, searching, and more. JavaScript arrays are extensively used to store and work with collections of related data, making them essential for tasks like data manipulation, list rendering, and implementing algorithms.

You can access elements in a JavaScript array using square brackets ([]) and the index of the element. Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on. For example, to access the third element of an array called myArray, you would use myArray[2]. To modify an element, you can simply assign a new value to the desired index. For example, myArray[1] = "new value" would change the value of the second element in the array. Additionally, JavaScript arrays provide methods like push(), pop(), splice(), and slice() for adding, removing, and manipulating elements in various ways.

To find the length of a JavaScript array, you can use the length property. The length property returns the number of elements in an array. For example, if you have an array called myArray, you can determine its length by accessing myArray.length. The length property is especially useful when working with loops or conditional statements that need to iterate over the entire array. It helps in dynamically determining the size of the array and performing operations based on the number of elements. Remember that the length property always returns one greater than the highest index in the array, as arrays are zero-indexed.