JavaScript array has more than one value at a time. In another word you can say an Array is a collection of similar types of elements.
Note: Array index is starts 0.
Syntax
Declaring an Array
let arr = new Array();
let arr = [];Creating an Array
var array_name = [item1, item2, ...]; Example JavaScript Array
Creating Array with values.
<html>
<head>
<title>Sample Code</title>
<script type="text/javascript">
var num = [1, 2, 3, 4, 5 ];
alert(num)
</script>
</head>
</html> Output:

Using the JavaScript Keyword new
<html>
<head>
<title>Sample Code</title>
<script type="text/javascript">
var num = new Array();
num[0] = "a";
num[1] = "b";
alert(num)
</script>
</head>
</html> Output: a,b
Advantage of Array
- JS Arrays represent multiple data items of the same type using a single name.
- In arrays, the elements can be accessed randomly by using the index number.
List of JavaScript array methods
Arrays provide a lot of methods. A cheat sheet of array methods in JavaScript:
- To add/remove elements:
push(...items)– adds items to the end,pop()– extracts an item from the end,shift()– extracts an item from the beginning,unshift(...items)– adds items to the beginning.splice(pos, deleteCount, ...items)– at indexposdeletedeleteCountelements and insertitems.slice(start, end)– creates a new array, copies elements from positionstarttillend(not inclusive) into it.concat(...items)– returns a new array: copies all members of the current one and addsitemsto it. If any ofitemsis an array, then its elements are taken.
- To search among elements:
indexOf/lastIndexOf(item, pos)– look foritemstarting from positionpos, return the index or-1if not found.includes(value)– returnstrueif the array hasvalue, otherwisefalse.find/filter(func)– filter elements through the function, return first/all values that make it returntrue.findIndexis likefind, but returns the index instead of a value.
- To iterate over elements:
forEach(func)– callsfuncfor every element, does not return anything.
- To transform the array:
map(func)– creates a new array from results of callingfuncfor every element.sort(func)– sorts the array in-place, then returns it.reverse()– reverses the array in-place, then returns it.split/join– convert a string to array and back.reduce(func, initial)– calculate a single value over the array by callingfuncfor each element and passing an intermediate result between the calls.
- Additionally:
Array.isArray(arr)checksarrfor being an array.
Q: How to get the JavaScript array length?
Answer: Using the length property you can get the length of an array (the number of array elements).
<html>
<head>
<title>Sample Code</title>
<script type="text/javascript">
var fruits = ["Banana", "Orange", "Apple", "Mango"];
alert(fruits.length);
</script>
</head>
</html> Output: 4
Do comment if you have any doubts and suggestion on this topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version