In JavaScript, an array is a special type of object that is used to store and organize a collection of values, which can be of any type – such as numbers, strings, or even other arrays. It is essentially an object that denotes a grouping of elements of the same type.
What makes arrays particularly useful is that they allow you to access and manipulate a set of related values using a single variable name, instead of having to create separate variables for each value.
For example, you could create an array to store a list of names like this:
let names = ['Alice', 'Bob', 'Charlie'];
Here, names
is an array that contains three string values. You can access individual values in the array using their index position, which starts at 0. So, to access the first name in the list (which is ‘Alice’), you would use names[0]
.

Arrays in JavaScript also have a number of built-in methods that allow you to perform operations like adding or removing elements, sorting, and filtering.
In JavaScript, there are three main ways to create an array:
1. Array literal syntax:
This is the most common way to create an array in JavaScript. You simply enclose a list of values in square brackets, separated by commas.
let myArray = [1, 2, 3, 4, 5];
2. Creating an instance of Array directly:
You can also create an empty array by calling the Array
constructor directly.
For example:
let myArray = new Array();
You can then add elements to the array using the push()
method:
myArray.push(1);
myArray.push(2);
myArray.push(3);
3. Using an Array constructor with a specified length:
You can also create an array with a specified length by calling the Array
constructor with an integer argument, like this:
let myArray = new Array(5);
This creates an empty array with a length of 5. You can then add elements to the array by assigning values to specific indexes:
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
JavaScript Array Methods:
JavaScript arrays come with a wide variety of built-in methods that allow you to perform different operations on the array elements. These methods can be broadly classified into the following categories:
- Mutator methods: These methods modify the array in place, adding, removing, or changing elements. Some examples of mutator methods include
push()
,pop()
,shift()
,unshift()
,splice()
, andsort()
. - Accessor methods: These methods do not modify the array, but instead return a new array or some other value based on the existing array. Some examples of accessor methods include
concat()
,slice()
,join()
,indexOf()
,lastIndexOf()
,includes()
, andtoString()
. - Iteration methods: These methods allow you to loop over the array elements and perform some operation on each element. Some examples of iteration methods include
forEach()
,map()
,filter()
,reduce()
, andfind()
.
Each of these method categories serves a different purpose and can be useful in different programming scenarios. Understanding these methods and how to use them effectively can help you write more efficient and streamlined code.
Let’s see the list of JavaScript array methods with their description.
Methods | Description |
---|---|
concat() | It returns a new array object that contains two or more merged arrays. |
copywithin() | It copies the part of the given array with its own elements and returns the modified array. |
entries() | It creates an iterator object and a loop that iterates over each key/value pair. |
every() | It determines whether all the elements of an array are satisfying the provided function conditions. |
flat() | It creates a new array carrying sub-array elements concatenated recursively till the specified depth. |
flatMap() | It maps all array elements via mapping function, then flattens the result into a new array. |
fill() | It fills elements into an array with static values. |
from() | It creates a new array carrying the exact copy of another array element. |
filter() | It returns the new array containing the elements that pass the provided function conditions. |
find() | It returns the value of the first element in the given array that satisfies the specified condition. |
findIndex() | It returns the index value of the first element in the given array that satisfies the specified condition. |
forEach() | It invokes the provided function once for each element of an array. |
includes() | It checks whether the given array contains the specified element. |
indexOf() | It searches the specified element in the given array and returns the index of the first match. |
isArray() | It tests if the passed value ia an array. |
join() | It joins the elements of an array as a string. |
keys() | It creates an iterator object that contains only the keys of the array, then loops through these keys. |
lastIndexOf() | It searches the specified element in the given array and returns the index of the last match. |
map() | It calls the specified function for every array element and returns the new array |
of() | It creates a new array from a variable number of arguments, holding any type of argument. |
pop() | It removes and returns the last element of an array. |
push() | It adds one or more elements to the end of an array. |
reverse() | It reverses the elements of given array. |
reduce(function, initial) | It executes a provided function for each value from left to right and reduces the array to a single value. |
reduceRight() | It executes a provided function for each value from right to left and reduces the array to a single value. |
some() | It determines if any element of the array passes the test of the implemented function. |
shift() | It removes and returns the first element of an array. |
slice() | It returns a new array containing the copy of the part of the given array. |
sort() | It returns the element of the given array in a sorted order. |
splice() | It add/remove elements to/from the given array. |
toLocaleString() | It returns a string containing all the elements of a specified array. |
toString() | It converts the elements of a specified array into string form, without affecting the original array. |
unshift() | It adds one or more elements in the beginning of the given array. |
values() | It creates a new iterator object carrying values for each index in the array. |
An arrays are a fundamental data structure in JavaScript that allow you to store and organize a collection of values.
JavaScript arrays are dynamic, which means that they can grow or shrink in size as needed. They can also hold values of different types. Which making them a versatile tool for a wide range of programming tasks.
Arrays in JavaScript are objects, with a number of built-in methods and properties that allow you to perform operations like adding or removing elements, sorting, and filtering. Some of the most commonly used array methods include push()
, pop()
, shift()
, unshift()
, slice()
, and splice()
.
When creating arrays in JavaScript, there are several different approaches you can take, including using array literal syntax, creating an instance of Array
directly, or using an array constructor with a specified length. The choice of which method to use depends on your specific use case and coding preferences
Recommended:
What is variable in JavaScript?
How to define constant in java?
What is the Console in JavaScript.
What are the basic SQL Command ?
What is array reduce()method in JavaScript?