Before we go to the array methods, we should know about JavaScript Array. A JavaScript array holds more than one value in a single variable. Like other arrays, the JavaScript array index starts from 0. Array stores similar types of data. We can fetch the values from JavaScript array using an index numbers. Array definitions are allocated memory in a sequence.
We can declare arrays in two ways. The following are the ways.
There is no difference between the above two methods. For faster performance and readability, we don't need the new Array() method.
List of Array methods
The followings are the JavaScript array methods.
concat()
It concatenates more than one array and returns the new joined array.
Example
- <html>
- <body>
- <button onclick="concatArray()">Click</button>
- <p id="pDemo"></p>
- <script>
- function concatArray() {
- var colors=["Red","Green","Blue"];
- var friends=["Bibhu","Omm","Niladri"];
- var Office=["Mindtree"];
- var concat= Office.concat(colors,friends);
- document.getElementById("pDemo").innerHTML = concat;
- }
- </script>
- </body>
- </html>
copyWithin()
Using this method, we can copy the array elements from one position to a different position, overwriting with the existing value.
Example
- <html>
- <body>
- <button onclick="arrayCopyWithin()">Click</button>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- function arrayCopyWithin() {
- document.getElementById("pDemo").innerHTML = colors.copyWithin(1,0);
- }
- </script>
- </body>
- </html>
entries()
We can insert another key/value for each element of the array. For example - we can insert the index method to the array.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _color = colors.entries();
- for (x of _color) {
- document.getElementById("pDemo").innerHTML += x + "
- <br>";
- }
-
- </script>
- </body>
- </html>
every()
It checks if all methods pass a test or not.
Example
- <html>
- <body>
- <button onclick="getEveryValueCondition()">Click</button>
- <p id="pDemo"></p>
- <script>
- var marks = [70, 30, 35, 50];
- function checkPass(markList ) {
- return markList >= 40;
- }
- function getEveryValueCondition() {
- document.getElementById("pDemo").innerHTML = marks .every(checkPass);
- }
- </script>
- </body>
- </html>
fill()
All array elements will override with a specific element. It works as a static value.
Example
- <html>
- <body>
- <button onclick="fillTheArray()">Click</button>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri"];
- function fillTheArray() {
- document.getElementById("pDemo").innerHTML = friends.fill("Pradosh");
- }
- </script>
- </body>
- </html>
filter()
It returns the elements that pass a specific condition.
Example
- <html>
- <body>
- <button onclick="filterArray()">Click</button>
- <p id="pDemo"></p>
- <script>
- var marks= [70, 30, 35, 55];
- function filterMarks(marks) {
- return marks >= 37;
- }
- function filterArray() {
- document.getElementById("pDemo").innerHTML = marks.filter(filterMarks);
- }
- </script>
- </body>
- </html>
find()
Returns the fist passed element.
Example
- <html>
- <body>
- <button onclick="findArray()">Click</button>
- <p id="pDemo"></p>
- <script>
- var marks = [70,37, 50, 38];
- function checkMarks(marks) {
- return marks >= 35;
- }
- function findArray() {
- document.getElementById("pDemo").innerHTML = marks.find(checkMarks);
- }
- </script>
- </body>
- </html>
findIndex()
Finds the index of the first passed element.
Example
- <html>
- <body>
- <button onclick="checkMarks()">Click</button>
- <p id="pDemo"></p>
- <script>
- var marks = [25,35,45,55];
- function chcekPass(marks) {
- return marks >= 37;
- }
- function checkMarks() {
- document.getElementById("pDemo").innerHTML = marks.findIndex(chcekPass);
- }
- </script>
- </body>
- </html>
forEach()
Iterates the array till the array index doesn't finish.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- colors.forEach(forEachFunction);
- function forEachFunction(item, index) {
- document.getElementById("pDemo").innerHTML += index + ":" + item + "
- <br>";
- }
-
- </script>
- </body>
- </html>
from()
It creates an array from a string.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var array = Array.from("BIDYASAGAR");
- document.getElementById("pDemo").innerHTML = array;
- </script>
- </body>
- </html>
includes()
Finds the given value in an array. Returns true/false
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= colors.includes("Green");
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
indexOf()
Finds the given element's index number
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= colors.indexOf("Green");
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
isArray()
It returns true if the variable is an array.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= Array.isArray(colors);
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
join()
It returns an array as a string.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= colors.join();
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
keys()
Creates an iteration object from the array, Holds the index keys.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors = colors.keys();
- for (items of _colors) {
- document.getElementById("pDemo").innerHTML += items + "
- <br>";
- }
-
- </script>
- </body>
- </html>
lastIndexOf()
Holds the last index of an array element.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= colors.lastIndexOf("Blue");
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
valueOf()
Returns the value of an array
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- var _colors= colors.valueOf("Blue");
- document.getElementById("pDemo").innerHTML = _colors;
- </script>
- </body>
- </html>
map()
It will loop each element of the array. Inside map(), we will assign another task. Each element will run with that task.
Example
- <html>
- <body>
- <button onclick="mapMarks()">Click</button>
- <p id="pDemo"></p>
- <script>
- var marks=[25,35,45,55];
- function mapMarks() {
- numbers= document.getElementById("pDemo")
- numbers.innerHTML = marks.map(Math.sqrt);
- }
- </script>
- </body>
- </html>
pop()
Deletes the last element of an array.
Example
- <html>
- <body>
- <button onclick="popFunction()">Clear</button>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri"];
- function popFunction() {
- friends.pop();
- document.getElementById("pDemo").innerHTML = friends;
- }
- </script>
- </body>
- </html>
push()
Inserts item as the last element of the array.
Example
- <html>
- <body>
- <button onclick="pushFunction()">Add</button>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri"];
- function pushFunction() {
- friends.push("Pradosh");
- document.getElementById("pDemo").innerHTML = friends;
- }
- </script>
- </body>
- </html>
reduce()
Reduces the array to a single value using left to right order.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var marks=[525,43,45]
- document.getElementById("pDemo").innerHTML = marks.reduce(reduceFunction);
- function reduceFunction(total, num) {
- return total - num;
- }
- </script>
- </body>
- </html>
reduceright()
Reduces the array to a single value using the right to left order.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var marks=[525,43,45]
- document.getElementById("pDemo").innerHTML = marks.reduceRight(reduceRightFunction);
- function reduceRightFunction(total, num) {
- return total - num;
- }
- </script>
- </body>
- </html>
reverse()
Reverses the order of array elements.
Example
- <html>
- <body>
- <button onclick="reverseColors()">Reverse</button>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- document.getElementById("pDemo").innerHTML = fruits;
- function reverseColors() {
- colors.reverse();
- document.getElementById("pDemo").innerHTML = colors;
- }
- </script>
- </body>
- </html>
shift()
Deletes the first element of the array and returns the rest amount.
Example
- <html>
- <body>
- <button onclick="shiftFunction()">Shift</button>
- <p id="pDemo"></p>
- <script>
- var colors=["Red","Green","Blue"];
- function shiftFunction() {
- colors.shift();
- document.getElementById("pDemo").innerHTML = colors;
- }
- </script>
- </body>
- </html>
slice()
Selects an element from the array using index range.
Example
- <html>
- <body>
- <button onclick="sliceFunction()">Slice</button>
- <p id="pDemo"></p>
- <script>
- function sliceFunction() {
- var colors=["Red","Green","Blue","Yellow"];
- var sliceColor = colors.slice(2, 3);
- document.getElementById("pDemo").innerHTML = sliceColor;
- }
- </script>
- </body>
- </html>
some()
Checks if given elements are available or not.
Example
- <html>
- <body>
- <button onclick="someFunction()">Some</button>
- <p id="pDemo"></p>
- <script>
- var marks=[25,35,45,55]
- function checkMark(mark) {
- return mark >= 18;
- }
- function someFunction() {
- document.getElementById("pDemo").innerHTML = marks.some(checkMark);
- }
- </script>
- </body>
- </html>
sort()
Used for sorting the array elements.
Example
- <html>
- <body>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri"];
- friends.sort();
- document.getElementById("pDemo").innerHTML = friends;
- </script>
- </body>
- </html>
splice()
Removes and adds elements to the array.
Example
- <html>
- <body>
- <button onclick="spliceFunction()">Splice</button>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri","Pradosh"];
- function spliceFunction() {
- friends.splice(2, 0, "Sambit", "Bata");
- document.getElementById("pDemo").innerHTML = friends;
- }
- </script>
- </body>
- </html>
tostring()
Converts array into a string.
Example
- <html>
- <body>
- <button onclick="toStringFunction()">String</button>
- <p id="pDemo"></p>
- <script>
- function toStringFunction() {
- var friends=["Bibhu","Omm","Niladri"];
- var _friends = friends.toString();
- document.getElementById("pDemo").innerHTML = _friends;
- }
- </script>
- </body>
- </html>
unshift()
Insert new items to the array in the beginning.
Example
- <html>
- <body>
- <button onclick="unshiftFunction()">Unshift</button>
- <p id="pDemo"></p>
- <script>
- var friends=["Bibhu","Omm","Niladri"];
- function unshiftFunction() {
- friends.unshift("Bidya", "Sambit");
- document.getElementById("pDemo").innerHTML = friends;
- }
- </script>
- </body>
- </html>
Summary
In this session, I discussed the commonly used JavaScript array methods. I hope this session will help beginners.