CodesJava

Easy learning with example program codes

JavaScript Array

An array is a single variable which can store multiple values of same type at a time. Thus, Javascript array is used to hold many values of same type under a single name. These values can be accessed then 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 instance of 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>

Output:

Jai
Navdeep
Shivanshu
Lalit

JavaScript Array Object Properties:

PropertyDescription
constructorReturns a reference to the array function that created the object.
lengthIt reflects the number of elements in an array.
prototypeIt 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 returns the modified array.

Array every() method:
Use: To determine whether all the elements of an array are satisfying 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 returns 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 returns the index of the last match.

Array map() method:
Use: To call the specified function for every array element and returns the new array.

Array pop() method:
Use: To remove and returns 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 given array.

Array shift() method:
Use: To remove and returns 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 in the beginning of the given array.

Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status