Common array javascript methods every developer must know

var marks = new Array();
marks = [100,80,90]Array.prototype.pop()

The pop() method removes the last element from an array and returns thus changing the length of the array.
marks.pop(); // console. log(marks) will display 100, 90 as values in the new array.

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
marks.unshift(52)// this will add 52 at the begining of the the array

The push() method adds one or more elements at the end of an array
cars.push( 92, 93);
console.log(cars); // // console. log(marks) will add 92, 93 as values at the end the marks array.

The shift() method will removes the first element from the array and returns that removed element thus changing the length of the array.
marks.shift();// This removes 100 from our marks array.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

let distinctions = marks.filter(x=>x>75) // this will create a new array containing values that exceed 75.

The concat() method merge two or more arrays there by creating a new array.
firstClass = [52,56,89,]
secondClass = [89,90]
combinedClass = firstClass.concat(secondClass);

Leave a Reply

Your email address will not be published.