array in js
- Published
- Reading time
- 3 min read
- Length
- 480 words
On this page
Array in JavaScript#
Arrays are a type of object in JavaScript that can store multiple values in a single variable. In other words, it is a collection of data items of the same type stored in contiguous memory locations. Each item in an array is called an element and is accessed by its index number.
Creating an Array#
To create an array, we can use the array literal notation, which is a comma-separated list of values enclosed in square brackets:
var fruits = ["apple", "banana", "orange"];We can also create an array using the Array() constructor:
var cars = new Array("BMW", "Mercedes", "Audi");Note that the Array() constructor can also take a single argument representing the length of the array to be created.
Accessing Array Elements#
We can access the elements of an array using their index numbers. The first element has an index of 0, the second element has an index of 1, and so on. We can also use negative index numbers to access elements from the end of the array.
var fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // Output: "apple"
console.log(fruits[1]); // Output: "banana"
console.log(fruits[2]); // Output: "orange"
console.log(fruits[-1]); // Output: undefined
console.log(fruits[3]); // Output: undefined
console.log(fruits[fruits.length - 1]); // Output: "orange"Array Methods#
JavaScript provides many methods for working with arrays. Some of the commonly used methods are:
push(): adds one or more elements to the end of an arraypop(): removes the last element from an array and returns itshift(): removes the first element from an array and returns itunshift(): adds one or more elements to the beginning of an arrayslice(): returns a new array containing a portion of the original arraysplice(): removes or replaces existing elements and/or adds new elements to an array
Here's an example of using some of these methods:
var fruits = ["apple", "banana", "orange"];
console.log(fruits.length); // Output: 3
fruits.push("grape");
console.log(fruits); // Output: ["apple", "banana", "orange", "grape"]
console.log(fruits.length); // Output: 4
var lastFruit = fruits.pop();
console.log(lastFruit); // Output: "grape"
console.log(fruits); // Output: ["apple", "banana", "orange"]
console.log(fruits.length); // Output: 3
var firstFruit = fruits.shift();
console.log(firstFruit); // Output: "apple"
console.log(fruits); // Output: ["banana", "orange"]
console.log(fruits.length); // Output: 2
fruits.unshift("kiwi", "pear");
console.log(fruits); // Output: ["kiwi", "pear", "banana", "orange"]
console.log(fruits.length); // Output: 4
var citrusFruits = fruits.slice(2);
console.log(citrusFruits); // Output: ["banana", "orange"]
console.log(fruits); // Output: ["kiwi", "pear", "banana", "orange"]
var removedFruits = fruits.splice(1, 2, "cherry", "pineapple");
console.log(removedFruits); // Output: ["pear", "banana"]
console.log(fruits); // Output: ["kiwi", "cherry", "pineapple", "orange"]
console.log(fruits.length); // Output: 4These are just a few of the many methods available for working with arrays in JavaScript. Knowing how to use them can make your code more efficient and easier to read.