JavaScript Array

JavaScript array is an object that represents a collection of similar type of elements.
//Syntax
const myArr = ["ram"," mohan","shyam"];
console.log(myArr);
// Output: [ ram, mohan , shyam]
JavaScript Array Methods
Array.Prototype.at()
The at() method takes an integer value. and return the item at that index.//Example const array1 = [ 5, 12, 8, 130, 44]; const index = 2; const array2 = array1.at(index); console.log(array2); //Output : 8Array.Prototype.concat()
Theconcat()method is used to merge two or more arrays. this method doesn't change the existing arrays but instead returns a new array.const arr1 = [1,2,3]; const arr2 = [4,5,6]; const arr3 = arr1.concat(arr2); console.log(arr3); //output : [1, 2, 3, 4, 5, 6] const arr4 = arr2.concat(arr1); //output : [4, 5, 6, 1, 2, 3]Array.Prototype.every()
Theevery()method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.const arr1 = [1, 2, 3 ,4 ,5]; const arrCheck = (num) => num<10; console.log(arr1.every(arrCheck)); //Output : true const arr2 = [1, 4, 5, 12, 17]; const arrCheck = (num) => num<10; console.log(arr2.every(arrCheck)); //Output : FalseArray.Prototype.fill()
Thefill()method changes all elements in an array to a static value, from a start index (default0) to an end index (defaultarray.length). It returns the modified array.const arr1 = [1, 2, 3 ,4 ,5]; console.log(arr1.fill(6)); //output : [6, 6, 6, 6, 6] console.log(arr1.fill(6,2)); //output : [1 ,2, 6, 6, 6] console.log(arr1.fill(6,1,3)) //output : [1, 6, 6, 6, 5]Array.Prototype.find()
Thefind()method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function,undefinedis returned.const array1 = [5, 12, 8, 57, 43]; const found = array1.find(element => element > 10); console.log(found); // output: 12Array.Prototype.filter()
The
filter()method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.const array1 = [5, 12, 8, 57, 43]; const found = array1.filter(element => element > 10); console.log(found); //output: [12, 57, 43]Array.Prototype.findIndex()
ThefindIndex()method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.const array1 = [5, 12, 8,45, 57, 43]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); //output : 3Array.Prototype.findLastIndex()
ThefindLastIndex()method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.const array1 = [5, 62, 8, 55, 57, 4]; const isLargeNumber = (element) => element > 50; console.log(array1.findIndex(isLargeNumber)); //output : 4Array.Prototype.flat()
Theflat()method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.const arr1 = [0, 1, 2, [3, 4]]; console.log(arr1.flat()); // output: Array [0, 1, 2, 3, 4] const arr2 = [0, 1, 2, [[[3, 4]]]]; console.log(arr2.flat(2)); // output: Array [0, 1, 2, Array [3, 4]] console.log(arr2.flat(3)) //output [0, 1, 2, 3, 4]Array.Prototype.forEach()
TheforEach()method executes a provided function once for each array element of the given array.const arr1 = [ 1, 2, 3, 4]; const mult = array1.forEach(element => element*2); console.log(mult); //output : [2, 4, 6, 8]Array.Prototype.includes()
The
includes()method determines whether an array includes a certain value among its entries, returningtrueorfalseas appropriate.const array1 = [1, 2, 3]; console.log(array1.includes(6)) //false console.log(array1.includes(2)) //trueArray.Prototype.indexOf()
The
indexOf()method returns the first index at which a given element can be found in the array, or -1 if it is not present.const array1 = [5, 62, 8, 55, 57, 55]; console.log(array1.indexOf(55)); // 3 console.log(array1.indexOf(65)); // -1 console.log(array1.indexOf(55,4)); // 5Array.Prototype.join()
Thejoin()method creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.const elements = ['Fire', 'Air', 'Water']; console.log(elements.join()); // output: "Fire,Air,Water" console.log(elements.join('')); // output: "FireAirWater" console.log(elements.join('-')); // output: "Fire-Air-Water"Array.Prototype.lastIndexOf()
The
lastIndexOf()method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backward, starting atfromIndex.const array1 = [5, 62, 8, 55, 57, 55]; console.log(array1.lastIndexOf(55)); // 5 console.log(array1.lastIndexOf(65)); // -1Array.Prototype.map()
The
map()method creates a new array populated with the results of calling a provided function on every element in the calling array.const array1 = [2 ,3, 5, 7, 8] const map1 = array1.map(x => x * 2); console.log(map1); // output: Array [4, 6, 10, 14 , 16]Array.Prototype.pop()
The
pop()method removes the last element from an array and returns that element. This method changes the length of the array.const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; console.log(plants.pop()); // output : "tamato" console.log(plants); // output: Array ["broccoli", "cauliflower", "cabbage", "kale"]Array.Prototype.push()
The
push()method adds one or more elements to the end of an array and returns the new length of the array.const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale']; const counts = plants.push("tamato")); console.log(counts); // output : 5 console.log(plants); //output: Array ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];Array.Prototype.reverse()
Thereverse()method reverses an array and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.const array1 = ['one', 'two', 'three']; const reversed = array1.reverse(); console.log( reversed ); //Array ["three", "two", "one"]Array.Prototype.shift()
The
shift()method removes the first element from an array and returns that removed element. This method changes the length of the array.const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; console.log(plants.shift()); // output : "broccoli" console.log(plants); // output: Array ["cauliflower", "cabbage", "kale", "tamato"]Array.Prototype.unShift()
Theunshift()method adds one or more elements to the beginning of an array and returns the new length of the array.const array1 = [1, 2, 3]; console.log(array1.unshift(4, 5)); // output: 5 console.log(array1); // output: Array [4, 5, 1, 2, 3]Array.Prototype.some()
Thesome()method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.const array = [3, 2, 7, 4, 5]; const even = (element) => element % 2 === 0; console.log(array.some(even)); // Expected output: trueArray.Prototype.slice()
Theslice()method returns a shallow copy of a portion of an array into a new array object selected fromstarttoend(endnot included) wherestartandendrepresent the index of items in that array. The original array will not be modified.const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2)); // output: Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)); // output: Array ["camel", "duck"] console.log(animals.slice(1, 5)); // output: Array ["bison", "camel", "duck", "elephant"] console.log(animals.slice(-2)); // output: Array ["duck", "elephant"] console.log(animals.slice(2, -1)); // output: Array ["camel", "duck"] console.log(animals.slice()); // output: Array ["ant", "bison", "camel", "duck", "elephant"]Array.Prototype.splice()
Thesplice()method changes the contents of an array by removing or replacing existing elements and/or adding new elements.const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); // Inserts at index 1 console.log(months); // output: Array ["Jan", "Feb", "March", "April", "June"] months.splice(4, 1, 'May'); // Replaces 1 element at index 4 console.log(months); // output: Array ["Jan", "Feb", "March", "April", "May"]Array.Prototype.toString()
The
toString()method returns a string representing the specified array and its elements.const array1 = [1, 2, 'a', '1a']; console.log(array1.toString()); // output: "1,2,a,1a"Array.isArray()
The
Array.isArray()static method determines whether the passed value is anArray.console.log(Array.isArray([1, 3, 5])); // output: true console.log(Array.isArray('[]')); // output: falseArray.Prototype.reduce()
Thereduce()method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.const array1 = [1, 2, 3, 4, 5]; // 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array1.reduce( (accumulator, currentValue) => accumulator + currentValue, initialValue ); console.log(sumWithInitial); // Expected output: 15
Comments
Join the discussionNo comments yet. Be the first to comment.


