JavaScript: Important Array Methods

JavaScript: Important Array Methods

JavaScript Array Methods – Today we are going to show you mostly used and important JavaScript Array Methods. Javascirpt array methods help the developers to write clean code with less effort.

Some of the most important and useful javascript array mehtods like Javascript array push, javascript array filter, javascript array find, javascript array map, javascript array splice, javascript array reverse, javascript array sort, javascript array concat, javascript array shift, javascript array unshift etc.

JavaScript Array Methods

Contents

  • pop()
  • push()
  • toString()
  • join()
  • splice()
  • sort()
  • shift()
  • unshift()
  • reverse()
  • concat()
  • slice()
  • filter()
  • find()

pop()

The “pop()” method is used when you want to remove the last element of an array.

The method returns the removed items value.

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.pop();     
Output
// New array: PHP, Python, Java

push()

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

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.push("JavaScript");         
Output
// New array: PHP, Python, Java, C, JavaScript

toString()

The “toString()” method is used to conver array to string.

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.toString();
// output
PHP, Python, Java, C

join()

The join() used to join the elements of an array into a string.
The “join()” method puts all the elements of the array into a string list. This method difference from “toString()” method.

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.join(" - ");
// output
PHP - Python - Java - C

splice()

The “splice()” method can add and remove items form an array.

syntax
================
array.splice(index, howMany, [element1][, …, elementN]);
  • index − Index param specifies where a new item should be inserted.
  • howMany − An integer indicating the number of old array elements to remove.
  • If howMany set to 0, no items will be removed in array list.

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.splice(2, 0, "Javascript", "Rust"); 
// Output
new array: PHP, Python, Javascript, Rust, Java, C

sort()

This method either alphabetic or numeric sorts an array.

Example

 var lang = ["PHP", "Python", "Java", "C"];
 lang.sort();
// Outuput
array: C, Java, PHP, Python

shift()

This method removes the first element of an array. like the “pop()” method, removes an element from an array.

Example

 var lang = ["PHP", "Python", "Java", "C"]; 
 lang.shift(); 
// Output
new array: Python, Java, C

unshift()

The unshift() method adds a new element to the beginning of an array.

Example

 var lang = ["PHP", "Python", "Java", "C"]; 
 cars.unshift("JavaScript");     
//Output 
New array: JavaScript, PHP, Python, Java, C

reverse()

The method is used for reverses the order of the elements in an array.

Example

 var lang = ["PHP", "Python", "Java", "C"];             
 lang.reverse();   
// Outuput
array: C, Java, PHP, Python

concat()

The “concat()” method joins two or more arrays and makes a new one.

Example

 var lang = ["PHP", "Python", "Java", "C"];  
 var newlang = ["JavaScript", "Rust"];  
 var join = lang.concat(newlang);    
// Output
New array: PHP, Python, Java, C, JavaScript, Rust

slice()

The method is used to selected elements in an array and makes a new one. It can take one or two arguments.

Example

 var lang = ["PHP", "Python", "Java", "C", "JavaScript"];
 var lang = cars.slice(1, 4);  
//Output 
New Array : Python, JavaScript

filter()

This filter method is used to filter the array, according our conditions.

Example

<p>function isCheck(value) {<br>
  return value < 10;<br>
}

var filter = [10, 5, 16, 4, 7, 12].filter(isCheck);
Output
//new array: 5,4,7

In this example the function filter() creates a new array. Those elements that satisfy the condition checked by isCheck() function.

find()

This find method is used find the first element of an array.

Example

function isCheck(value) {
  return value >= 10;
}

var find= [10, 5, 16, 4, 7, 12].find(isCheck);
Output
// 12

Leave a Reply

Your email address will not be published. Required fields are marked *