JavaScript Array Methods

In this article we will learn about JavaScript Array Methods. Let’s get started :


Javascript array / js array


What are the properties of an array in JavaScript?


concat()

The concat() method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

var city = ["Surat", "Agra"]; var state = ["Gujarat", "Uttar Pradesh"]; var childern = city.concat(state); // Returns a new array

copyWithin()

copies array elements to another position in the array, overwriting the existing values. This method will never add more items to the array. Note: this method overwrites the original array.

var fruits =["Banana", "Orange", "Apple"]; fruits.copyWithin(2, 0); // overwrites the original array

entries()

The entries() method returns an Array Iterator object with key/value pairs..

var fruits =["Banana", "Orange", "Apple"]; var f = fruits.entries(); // Returns new object //{ // [0, "Banana"], // [1, "Orange"], // [2, "Apple"], //}

every()

The every() method checks if all elements in an array pass a test (provided as a function).

var ages =[32,22,16,24]; function checkAdult(age){ return age >=18; } ages.every(checkAdult); // checks every element returns => false // every() returns true if all elements in an array pass a test (provided as a function).

fill()

The fill() method fills the specified elements in an array with a static value. You can specify the position of where to start and end the filling. If not specified, all elements will be filled.

var fruits =["Banana", "Orange", "Apple"]; fruits.fill("Kiwi"); // returns new array => [Kiwi,Kiwi,Kiwi,Kiwi]

filter()

The filter() method creates an array filled with all array elements that pass a test (provided as a function).

var ages =[32,22,16,24]; function checkAdult(age){ return age >=18; } ages.filter(checkAdult); // returns new array => [32,22,24]

find()

The find() method returns the value of the first element in an array that pass a test (provided as a function).

var ages =[3,2,18,24]; function checkAdult(age){ return age >=18; } ages.find(checkAdult); // returns first accurance of value => 18

findIndex()

The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).

var ages =[3,2,18,24]; function checkAdult(age){ return age >=18; } ages.findIndex(checkAdult); // returns first index of value => 2

forEach()

The forEach() method calls a function once for each element in an array, in order.

let number = [5,4,12,2]; numbers.forEach(myFunction) function myFunction(item,index,arr){ arr[index] = item *10; } console.log(numbers); //Output = [50,40,120,20]

includes()

The includes() method determines whether an array contains a specified element..

// Check if an array includes "Banana", starting the search at position 3 var fruits = ["Banana", "Orange", "Apple","Kiwi"]; var n = fruits.includes("Banana", 3); console.log(n); //Output false

indexOf()

The indexOf() method searches the array for the specified item, and returns its position.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var n = fruits.indexOf("Banana"); console.log(n); //Output 0

isArray()

The isArray() method determines whether an object is an array. This Function returns true if the object is an array, and false if not.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var result = Array.isArray(fruits); console.log(result); //Output true

join()

Convert the elements of an array into a string. The join() method returns the array as a string.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var n = fruits.join(" and "); console.log(n); //Output Banana and Orange and and Apple and Kiwi

lastIndexOf()

The lastIndexOf() method searches the array for the specified item, and returns its position.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var n = fruits.lastIndexOf("Banana"); console.log(n); //Output 0

Javascript Array /JavaScript Array Methods


javascript array map()

The map() method creates a new array with the results of calling a function for every array element.

var numbers = [4,9,16,25]; var n = numbers.map(Math.sqrt) console.log(n) // Output = [2,3,4,5]

pop()

The pop() method removes the last element of an array, and returns that element.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.pop(); console.log(fruits); //Output ["Banana", "Orange", "Apple"]

push()

The push() method adds new items to the end of an array, and returns the new length.

var fruits = ["Banana", "Orange", "Apple"]; var length = fruits.push("Kiwi"); console.log(fruits) console.log(length) // Output = ["Banana", "Orange", "Apple", "Kiwi"] // 4

reduce()

The reduce() method executes a provided function for each value of the array (from left-to-right) and reduces the array to a single value.

var numbers = [15.5,2.3,1.1,4.7]; function sum(total,num){ return total + Math.round(num); } var result = numbers.reduce(sum,0); console.log(result) // Output = 24

reduceRight()

The reduceRight() method executes a provided function for each value of the array (from right-to-left) and reduces the array to a single value.

var numbers = [10,150,200]; function myFunction(total,num){ return total - num; } var result = numbers.reduceRight(myFunction); console.log(result) // Output = 40

reverse()

The reverse() method reverses the order of the elements in an array.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.reverse(); console.log(fruits); //Output ["Kiwi", "Apple", "Orange", "Banana" ]

some()

The some() method checks if any of the elements in an array pass a test (provided as a function). It executes the function once for each element present in the array

var ages = [3,5,18,20]; function checkAdult(age){ return age >= 18; } console.log(ages.some(checkAdult)) // Output true

slice()

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. It returns the selected elements in an array, as a new array object.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var cirtus = fruits.slice(1,3); console.log(cirtus); //Output ["Orange", "Apple" ]

shift()

The shift() method removes the first item of an array.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.shift(); console.log(fruits); //Output [ "Orange", "Apple", "Kiwi" ]

javascript array sort()

The sort() method sorts the items of an array.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.sort(); console.log(fruits); //Output [ "Apple", "Banana", "Kiwi", "Orange"]

splice()

The splice() method adds/removes items to/from an array, and returns the removed item(s).

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.splice(2,0, "Lemon" , "Almond"); console.log(fruits); //Output ["Banana", "Orange", "Lemon" , "Almond", "Apple", "Kiwi"]

toString()

The toString() method returns a string with all the array values, separated by commas.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var s = fruits.toString(); console.log(s); //Output = Banana, Orange, Apple, Kiwi

unshift()

The unshift() method adds new items to the beginning of an array, and returns the new length.

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.unshift("Lemon", "Pineapple"); console.log(fruits); //Output ["Lemon", "Pineapple", "Banana", "Orange", "Apple", "Kiwi"]

valueOf()

The valueOf() method returns the array. This method is the default method of the array object. Array.valueOf() will return the same as Array

var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; var f = fruits.valueOf(); console.log(f); //Output ["Banana", "Orange", "Apple", "Kiwi"]

Javascript array length


I hope you like this JavaScript Array Methods article.🙂


Also Read :