An array is a single variable that can store multiple values of the same type at a time. Thus, a Javascript array is used to hold many values of the same type under a single name. These values can be accessed by referring to their index number. An array in Javascript can be constructed in many ways including;
By using an Array literal
Syntax:
var arrayName = [value 1, value 2, …];
By creating an instance of Array directly
Syntax:
var arrayName = new Array();
By using an Array constructor
An array in Javascript can also be constructed by creating an instance of the array by passing arguments in a constructor.
var arrayName =new Array("element1","element2",..."elementn");
Example:
<html> <body> <script> var myTeam=new Array("Jai","Navdeep","Shivanshu","Lalit"); for (i=0;i<myTeam.length;i++){ document.write(myTeam[i] + "<br>"); } </script> </body> </html>
JavaScript Array Object Properties:
Property | Description |
constructor | Returns a reference to the array function that created the object. |
length | It reflects the number of elements in an array. |
prototype | It allows us to add properties and methods to an object. |
JavaScript Array Methods:
JavaScript Array concat() method:
Use: To return a new array object that contains two or more merged arrays.
Array copywithin() method:
Use: To copy the part of the given array with its own elements and return the modified array.
Array every() method:
Use: To determine whether all the elements of an array satisfy the provided function conditions.
Array fill() method:
Use: To fill elements into an array with static values.
Array filter() method:
Use: To return the new array containing the elements that pass the provided function conditions.
Array find() method:
Use: To return the value of the first element in the given array that satisfies the specified condition.
Array findIndex() method:
Use: To return the index value of the first element in the given array that satisfies the specified condition.
Array forEach() method:
Use: To invoke the provided function once for each element of an array.
Array includes() method:
Use: To check whether the given array contains the specified element.
Array indexOf() method:
Use: To search the specified element in the given array and return the index of the first match.
Array join() method:
Use: To join the elements of an array as a string.
Array lastIndexOf() method:
Use: To search the specified element in the given array and return the index of the last match.
Array map() method:
Use: To call the specified function for every array element and return the new array.
Array pop() method:
Use: To remove and return the last element of an array.
Array push() method:
Use: To add one or more elements to the end of an array.
Array reverse() method:
Use: To reverse the elements of the given array.
Array shift() method:
Use: To remove and return the first element of an array.
Array slice() method:
Use: To return a new array containing the copy of the part of the given array.
Array sort() method:
Use: To return the element of the given array in a sorted order.
Array splice() method:
Use: To add/remove elements to/from the given array.
Array unshift() method:
Use: To add one or more elements at the beginning of the given array.