JavaScript arrays are used to store multiple values in a single variable. They are a type of object, but with a length property that is an integer and a few other special properties, like the ability to access items using square bracket notation.
An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let arr = [1, 2, 3, 4, 5];
[/dm_code_snippet]
This creates an array with five elements, each of which is a number. You can also use an array literal to create an empty array by leaving off the elements:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let arr = [];
[/dm_code_snippet]
You can also create an array using the Array
constructor function, like this:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let arr = new Array(1, 2, 3, 4, 5);
[/dm_code_snippet]
This creates an array with the same elements as the array literal above.
You can access the elements of an array using square bracket notation and an index. Array indexes are zero-based, so the first element of an array is at index 0, the second element is at index 1, and so on. For example:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let arr = [1, 2, 3, 4, 5]; console.log(arr[0]); // prints 1 console.log(arr[1]); // prints 2 console.log(arr[2]); // prints 3
[/dm_code_snippet]
You can also use square bracket notation to set the value of an element in an array:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let arr = [1, 2, 3, 4, 5]; arr[0] = 10; console.log(arr); // prints [10, 2, 3, 4, 5]
[/dm_code_snippet]
In addition to the elements of an array, there are several properties and methods that are available on arrays. Some of the most commonly used ones include:
length
: This property returns the number of elements in the array.push()
: This method adds one or more elements to the end of an array and returns the new length of the array.pop()
: This method removes the last element of an array and returns that element.shift()
: This method removes the first element of an array and returns that element.unshift()
: This method adds one or more elements to the beginning of an array and returns the new length of the array.concat()
: This method returns a new array that is the result of concatenating the given arrays.slice()
: This method returns a new array that is a copy of a portion of the original array.splice()
: This method modifies the contents of an array by removing existing elements and/or adding new elements.
There are many other properties and methods available on arrays, and you can find a full list in the documentation.
Arrays are a very useful and powerful feature of JavaScript, and they are used in a wide variety of applications. With their ability to store multiple values in a single variable, and the many methods and properties available for manipulating those values, they can help you write more efficient and effective code.