Must to know Javascript Arrays and Array.methods

In this article we are going to discuss about the JavaScript Arrays .
Arrays are special data type of JavaScript . I
t wont fall under JavaScript primitive data type category .
It might create lot of confusion for beginners . But the arrays are different kind of data type in JavaScript .

Lets go through some of the most important methods of Arrays in Javascript .

Javascript Array.length –
Array.length will help you to find out the total values count in a an array .
Example :

var students = [“Murali”,”Krishna”,”Ram”,”Ravi”]
In this example , if you check students.length , it will return 4

Javascript Array.pop()
Array.pop will remove the last value from a array
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”]
var deleted = students.pop()
if you check students it will return [“Murali”,”Krishna”,”Ram”];
if you check deleted it will return “Ravi” ;

Javascript Array.push();
Array.push() will ad a value to array ;
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.push(“Kiran”)
if you check students it will return [“Murali”,”Krishna”,”Ram”,”Ravi”, “Kiran” ;

Javascript Array.shift()

Array.shift() will remove first value from an Array .
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.shift()
if you check students it will return [“Krishna”,”Ram”,”Ravi”, “Kiran” ;

Javascript Array.sort()
Array.sort() will sort the given value in Array .
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.sort()
if you check students it will return  [“Krishna”, “Murali”, “Ram”, “Ravi”];

Javascript Array.unshift();
Array.unshift() will add a value in begining of Array .
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.unshift(“Robert”)
if you check students it will return  [“Robert”, “Murali”, “Krishna”, “Ram”, “Ravi”];

Javascript array.map()
Array.map will loop through all the values in given Array .
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.map((ele)=>{
console.log(ele)
})

This method will display all the values in Console

Javascript array.indexOf()

Array.indexOf() method will help us to find the given value is exist or not in the Array
Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
students.indexOf(“Ram”) will return 2; Because “Ram” esist in the index position 2 . It will return -1 if the value is not exist in the Array .

Javascript array.filter();
Array.filter() will go through all the elements in the given array .
We need to pass a call back function which will take an element as input.
we can add a condition in that call back function . So if the given condition is satisfied it will return the value.
if the condition doesnt met then the value will be ignored .

Example :
var students = [“Murali”,”Krishna”,”Ram”,”Ravi”] ;
Lets pass a call back function with a condition . It should return return the values whose length is more than 5
var newArray = students.filter(function (ele){
return ele.length > 5
})
It should return [“Robert”, “Murali”, “Krishna”]

Javascript forEach()
Array.map() will go through all the elements in the given array .

Javascript some()

Array.some() method tests whether at least one element in the array passes the test implemented by the provided function.;

var array = [1, 2, 3, 4, 5];

var even = function(element) {
// checks whether an element is even
return element % 2 === 0;
};

console.log(array.some(even));
// expected output: true

About Sunline IT Solutions : We are located at Marathahalli, Bangalore . We undertake online and offline full stack development Training . We are conducting 8 to 10 Weeks Boot camp program on Full-stack development . With over 10 years experience in various front technologies , we are committed to deliver as per the industry expectations . If you are Looking for a Training institutes around Marathahalli , this is the perfect place to start .

Leave a Reply

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