JavaScript Array Methods

Posted By : Swati Rahangdale | 28-Apr-2018

An Array object allows to store multiple values in a single variable. It contain a fixed-size sequential collection of elements of the same type. An array method is used to collection of data, but it is often more useful to determine of an array as a collection of variables of the same type.

 

Syntax:

syntax to create an Array object given below:

var fruits = new Array( "orange", "papaya", "apple" );

An Array parameter is a list of strings or integers. When you specify a single numeric parameter with an Array constructor, specify the initial length of an array. The maximum length for an array is 4,294,967,295.

 

Array Method object:

These are the follwing list of the Array method object:

1.concat()

The concat() method returns a new array consist of this array combined with two or more arrays.

Syntax

The syntax of concat() method is as follows −

array.concat(12, 13, ..., 15);

 

Example:
<html>
<head>
<title> concat Method</title>
</head>
<body>
<script type="text/javascript">
var alpha = ["ab", "bc", "cd"];
var numeric = [2, 1, 3];
var alphaNumeric = alpha.concat(numeric);
document.write("alphaNumeric : " + alphaNumeric );
</script>
</body>
</html>

2.map()

 The map() method creates a new array with the results to call a provided function on every element in this array.

Syntax

syntax is given below:

array.map(callback[, thisObject]);

3. pop()

The pop() method is used to remove the last element from an array and returns that element.

 

Example:
<html>
<head>
<title> pop Method</title>
</head>
<body>
<script type="text/javascript">
var numbers = [2, 6, 10];
var element = numbers.pop();
document.write("element is : " + element );
var element = numbers.pop();
document.write("<br />element is : " + element );
</script>
</body>
</html>

o/p:10

 

4.push()

The push() method is used to add a new element at the end of an array.

 

Example:
<html>
<head>
<title> push Method</title>
</head>

<body>
<script type="text/javascript">
var numbers = new Array(2, 4, 10);
var length = numbers.push(11);
document.write("new numbers is : " + numbers );
length = numbers.push(20);
document.write("<br />new numbers is : " + numbers );
</script>
</body>
</html>

o/p:new numbers is : 2,4,10,11

 

5. shift()

The shift() method is used to remove the first array element and ramaning elements shifts to a lower index.

 

Example:
<html>
<head>
<title> shift Method</title>
</head>

<body>

<script type="text/javascript">
var element = [50, 2, 5, 6].shift();
document.write("Removed element is : " + element );
</script>

</body>
</html>
o/p:50

 

About Author

Author Image
Swati Rahangdale

Swati has good konwledge of HTML,CSS, Boostrap.now . She is a UI developer. Her hobbies are watching movie.

Request for Proposal

Name is required

Comment is required

Sending message..