JavaScript arrays are a special kind of object that can store multiple values in a single variable. The values are called elements and are indexed, meaning they have a numerical position in the array. The index of the first element is 0, the second element has an index of 1, and so on.
You can create an array in JavaScript by using the []
brackets, and you can store values in an array by assigning them to a specific index. Here’s an example of creating an array and storing values in it:
[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 myArray = []; // create an empty array myArray[0] = "Hello"; // store the value "Hello" at index 0 myArray[1] = "World"; // store the value "World" at index 1
[/dm_code_snippet]
You can also use the Array()
constructor to create an array and initialize it with values. Here’s an 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 myArray = new Array("Hello", "World");
[/dm_code_snippet]
You can also use the Array Literal notation, which is the most common way:
[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 myArray = ["Hello", "World"];
[/dm_code_snippet]
You can access the values stored in an array by using the array’s name followed by the index of the value in square brackets. Here’s an example of accessing the values stored in the array we created above:
[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”]
console.log(myArray[0]); // prints "Hello" console.log(myArray[1]); // prints "World"
[/dm_code_snippet]
JavaScript arrays also have built-in methods for adding, removing, and manipulating elements, such as push()
, pop()
, shift()
, unshift()
, splice()
, slice()
, sort()
, reverse()
etc.
Additionally, you can use for loops to iterate over the elements in an array and perform some operation.
[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”]
for (let i = 0; i < myArray.length; i++) { console.log(myArray[i]); }
[/dm_code_snippet]
The above is simple example of how to store multiple values in one variable using JavaScript Arrays. There are many other things you can do with arrays, such as use them with higher-order functions like map()
, filter()
, reduce()
and many more.
Here are some additional details about JavaScript arrays:
- JavaScript arrays are dynamic, meaning they can grow or shrink in size as you add or remove elements. The
length
property of an array always reflects the number of elements in the array. - JavaScript arrays are also versatile, meaning you can store elements of different data types in a single array. For example, you can store numbers, strings, and objects in the same array.
- JavaScript arrays also have a number of built-in methods that you can use to add, remove, and manipulate elements. Some of the most commonly used methods include:
push()
andpop()
: These methods add and remove elements at the end of an array, respectively.shift()
andunshift()
: These methods remove and add elements at the beginning of an array respectively.splice()
: This method can add or remove elements from any position in an array.slice()
: This method returns a new array containing a subset of the elements from the original array.sort()
: This method sorts the elements in an array in ascending order.reverse()
: This method reverses the order of the elements in an array.concat()
: This method creates a new array by concatenating two or more arrays.
- JavaScript also provides the ability to iterate through the array using forEach, map, filter and reduce higher order functions.
forEach()
: This method calls a provided function once for each element in an array, in order.map()
: This method creates a new array with the results of calling a provided function on every element in the calling array.filter()
: This method creates a new array with all elements that pass the test implemented by the provided function.reduce()
: This method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
- JavaScript also provide an Spread operator, this allows us to expand array and retrieve element easily
...
: This is used to spread the elements of an array in a function call or to extract elements of an array as variables.
[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”]
function sum(a, b, c) { return a + b + c; } let numbers = [1, 2, 3]; console.log(sum(...numbers)); // 6 ```
[/dm_code_snippet]
That’s a broad overview of some of the key features and capabilities of JavaScript arrays. As you continue to work with arrays, you’ll likely discover many more ways to use them to accomplish a wide variety of tasks.