Now Reading: What is array in JavaScript?

Loading

What is array in JavaScript?

svgMarch 18, 2023Javascriptleetcode

What is an Array in JavaScript?

Arrays are an incredibly useful data structure in JavaScript, allowing developers to store and manipulate multiple values in a single variable. Compared to a variable that can store only one value, an array can store multiple values in a single variable, and each item in the array has a number attached to it, called a numeric index, that allows you to access it. In this article, we’ll take a look at what an array is in JavaScript, how to create an array, and how to use various methods to manipulate arrays.

What is an Array?

An array is a special type of variable that stores multiple values in a single variable. This is in contrast to a regular variable, which can only store one value at a time. Arrays are particularly useful when you need to store a list of items, such as names, addresses, or numbers.

Arrays are a type of object in JavaScript, which means they have properties and methods associated with them. The most common way to create an array is to use the array literal syntax, which is a set of square brackets surrounding a list of values separated by commas.

Creating an Array

There are several ways to create an array in JavaScript. The most common way is to use the array literal syntax, which is a set of square brackets surrounding a list of values separated by commas. For example, the following code creates an array with three items:

var myArray = [“John”, “Paul”, “George”];

You can also create an array using the Array constructor, which takes a list of arguments and creates an array from them. For example, the following code creates an array with three items:

var myArray = new Array(“John”, “Paul”, “George”);

Finally, you can create an empty array using the Array constructor with no arguments. This creates an array with no items in it.

var myArray = new Array();

Accessing Array Elements

Once you’ve created an array, you can access its elements using the array’s numeric index. Each item in an array has a number attached to it, called a numeric index, that allows you to access it. The first item in an array has an index of 0, the second item has an index of 1, and so on.

For example, the following code accesses the first item in the array:

var firstItem = myArray[0];

You can also use negative numbers to access items from the end of the array. For example, the following code accesses the last item in the array:

var lastItem = myArray[-1];

Adding and Removing Array Elements

You can add and remove elements from an array using the push() and pop() methods. The push() method adds an element to the end of an array, and the pop() method removes the last element from an array.

For example, the following code adds an element to the end of an array:

myArray.push(“Ringo”);

And the following code removes the last element from an array:

myArray.pop();

You can also use the shift() and unshift() methods to add and remove elements from the beginning of an array. The shift() method removes the first element from an array, and the unshift() method adds an element to the beginning of an array.

For example, the following code adds an element to the beginning of an array:

myArray.unshift(“Ringo”);

And the following code removes the first element from an array:

myArray.shift();

Iterating Over an Array

You can iterate over an array using the forEach() method. The forEach() method takes a callback function as an argument, and it calls the callback function once for each element in the array.

For example, the following code logs each element in an array to the console:

myArray.forEach(function(element) {
console.log(element);
});

Conclusion

Arrays are an incredibly useful data structure in JavaScript, allowing developers to store and manipulate multiple values in a single variable. Arrays are a type of object in JavaScript, which means they have properties and methods associated with them. The most common way to create an array is to use the array literal syntax, which is a set of square brackets surrounding a list of values separated by commas. You can access array elements using the array’s numeric index, and you can add and remove elements from an array using the push(), pop(), shift(), and unshift() methods. Finally, you can iterate over an array using the forEach() method.

svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg
Quick Navigation
  • 01

    What is array in JavaScript?