Working with arrays in JavaScript

Ashish Kasama|December 7, 2022|3 Minute read|
Play
/ / Working with arrays in JavaScript

SHARE

facebooktwitterwhatsapplinkedin
facebook
twitter
whatsapp
linkedin

Arrays are a fundamental data structure in JavaScript, and they are used to store and manipulate a collection of values. An array is an ordered list of values, and each value in the array is called an element.

In JavaScript, you can create an array using the "array literal" syntax, which uses square brackets to enclose a list of values separated by commas. For example:

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

In this example, the array "numbers" contains five elements: 1, 2, 3, 4, and 5.

You can access the elements of an array using their index, which is the position of the element in the array. In JavaScript, arrays are zero-indexed, which means that the first element has an index of 0. For example:

console.log(numbers[0]); // outputs 1 console.log(numbers[4]); // outputs 5

You can also use loops to iterate over the elements of an array. For example:

for (var i = 0; i < numbers.length; i++) { console.log(numbers[i]);
}

This for loop will output each element of the "numbers" array to the console.

Arrays also have a number of built-in methods that can be used to manipulate the elements of the array. For example, you can use the "push" method to add an element to the end of the array:

numbers.push(6);

You can also use the "pop" method to remove the last element of the array:

numbers.pop();

These are just a few examples of the many ways you can work with arrays in JavaScript. Arrays are a powerful data structure that are used to store and manipulate collections of data in a variety of applications.

Check out the rest of our series on Javascript by reading our other articles:

Ashish Kasama

Co-founder & Your Technology Partner

One-stop solution for next-gen tech.

Related Blogs

The latest from our innovation team

SEE ALL