JavaScripit Arrays
Arrays are used to store multiple values in a single variable. They are a special type of object that can hold multiple values at once.
One of the most important data structures in JavaScript is the array, which is a collection of elements. In this tutorial, we will explore the basics of JavaScript arrays and the various array methods that can be used to manipulate them.
An array in JavaScript is a collection of elements enclosed in square brackets. Elements can be of any data type, including numbers, strings, and other arrays.
Basic Syntax
Here is the basic syntax for creating an array in JavaScript:
let fruits = ["apple", "banana", "cherry"];In this example, we have created an array called fruits that contains three elements: 'apple', 'banana', and 'cherry'.
Array Methods
JavaScript provides a number of built-in methods for working with arrays. Here are some of the most commonly used array methods:
length
The length property returns the number of elements in an array:
let fruits = ["apple", "banana", "cherry"];
console.log(fruits.length); // 3push
The push method adds one or more elements to the end of an array:
let fruits = ["apple", "banana", "cherry"];
fruits.push("date");
console.log(fruits); // ['apple', 'banana', 'cherry', 'date']pop
The pop method removes the last element from an array and returns it:
let fruits = ["apple", "banana", "cherry"];
let lastFruit = fruits.pop();
console.log(lastFruit); // 'cherry'
console.log(fruits); // ['apple', 'banana']shift
The shift method removes the first element from an array and returns it:
let fruits = ["apple", "banana", "cherry"];
let firstFruit = fruits.shift();
console.log(firstFruit); // 'apple'
console.log(fruits); // ['banana', 'cherry']unshift
The unshift method adds one or more elements to the beginning of an array:
let fruits = ["apple", "banana", "cherry"];
fruits.unshift("date");
console.log(fruits); // ['date', 'apple', 'banana', 'cherry']slice
The slice method returns a new array containing a subset of the elements of the original array:
let fruits = ["apple", "banana", "cherry", "date"];
let subset = fruits.slice(1, 3);
console.log(subset); // ['banana', 'cherry']splice
The splice method changes the contents of an array by removing or replacing existing elements and/or adding new elements:
let fruits = ["apple", "banana", "cherry", "date"];
fruits.splice(2, 1, "grape");
console.log(fruits); // ['apple', 'banana', 'grape', 'date']indexOf
The indexOf method returns the index of the first occurrence of a specified element in an array, or -1 if the element is not found:
let fruits = ["apple", "banana", "cherry", "date"];
let index = fruits.indexOf("cherry");
console.log(index); // 2includes
The includes method returns true if an array contains a specified element, and false otherwise:
let fruits = ["apple", "banana", "cherry", "date"];
let hasCherry = fruits.includes("cherry");
console.log(hasCherry); // trueThese are just a few examples of the many array methods available in JavaScript. You can find more information about array methods in the MDN Web Docs.
Conclusion
- Arrays are used to store multiple values in a single variable.
- Arrays are a special type of object that can hold multiple values at once.
- JavaScript provides a number of built-in methods for working with arrays, such as
push,pop,shift,unshift,slice,splice,indexOf, andincludes.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on