Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

‭No.

‬ ‭Method‬ ‭Description‬ ‭Example‬

‭1.‬ ‭push()‬ ‭ dds elements to the end of an array.‬


A v‭ ar array = [ 1, 2, 3 ];‬
‭Syntax‬‭: array.push(element1, element2, …);‬ ‭var returnValue = array.push(‬‭4, 5‬‭);‬
‭Output‬‭:‬
‭ eturns‬‭: Length of the updated array.‬
R ‭array = [ 1, 2, 3,‬‭4, 5‬‭]; // Updated array.‬
‭– Overwrites the original array.‬ ‭returnValue = 5 // Length of the array.‬

‭2.‬ ‭pop()‬ ‭ emoves the last element from an array.‬


R v‭ ar array = [ 1, 2,‬‭3‬‭];‬
‭Syntax‬‭: array.pop();‬ ‭var returnValue = array.pop();‬
‭Output‬‭:‬
‭ eturns‬‭: The removed element.‬
R ‭array = [ 1, 2 ]; // Updated array.‬
‭– Overwrites the original array.‬ ‭returnValue =‬‭3‬‭// Removed element.‬

‭3.‬ ‭unshift()‬ ‭ dds elements to the beginning of an array.‬


A v‭ ar array = [ 1, 2, 3 ];‬
‭Syntax‬‭: array.unshift(element1, element2, …);‬ ‭var returnValue = array.unshift(‬‭4, 5‬‭);‬
‭Output‬‭:‬
‭ eturns‬‭: Length of the updated array.‬
R ‭array = [‬‭4, 5‬‭, 1, 2, 3 ]; // Updated array.‬
‭– Overwrites the original array.‬ ‭returnValue = 5 // Length of the array.‬

‭4.‬ ‭shift()‬ ‭ emoves the first element from an array.‬


R v‭ ar array = [‬‭1‬‭, 2, 3 ];‬
‭Syntax‬‭: array.shift();‬ ‭var returnValue = array.shift();‬
‭Output‬‭:‬
‭ eturns‬‭: The removed element.‬
R ‭array = [ 2, 3 ]; // Updated array.‬
‭– Overwrites the original array.‬ ‭returnValue =‬‭1‬‭// Removed element.‬

‭5.‬ ‭concat()‬ ‭ eturns the first element in the array that satisfies a‬
R ‭var array1 = [‬‭1, 2‬‭];‬
‭provided testing function.‬
‭Syntax‬‭: array.find(function(currentValue, index,‬ ‭var array2 = [‬‭3, 4‬‭];‬
‭arr),thisValue);‬

‭ eturns‬‭: The value of the first element that passes the‬


R ‭var newArray = array1.concat(array2);‬
‭test.‬
‭Otherwise, it returns undefined.‬ ‭Output‬‭: newArray = [‬‭1, 2, 3, 4‬‭];‬

‭6.‬ ‭slice()‬ ‭ xtracts a section of an array.‬


E v‭ ar array = [ 1,‬‭2, 3‬‭, 4, 5 ];‬
‭Syntax‬‭: array.slice(start_position, end_position)‬ ‭var newArray = array.slice( 1, 3 );‬
‭– start_position = default is 0.‬ ‭Output‬‭:‬
‭– end_position = default is the last element.‬ ‭newArray = [‬‭2, 3‬‭];‬
‭array = [ 1,‬‭2, 3‬‭, 4, 5 ]; // original array.‬
‭ eturns‬‭: New array with extracted elements.‬
R
‭– Does not change the original array.‬ v‭ ar array = [ 1,‬‭2, 3, 4, 5‬‭];‬
‭var newArray = array.slice( 1 );‬
‭Output‬‭:‬
‭newArray = [‬‭2, 3, 4, 5‬‭];‬
‭array = [ 1,‬‭2, 3, 4, 5‬‭];; // original array.‬
‭7.‬ ‭splice()‬ J‭ oins all elements of an array into a string with a provided‬ v‭ ar array = [ 1, 2,‬‭3‬‭, 4, 5 ];‬
‭separator.‬ ‭var newArray = array.splice( 2, 1,‬‭‘a’, ‘b’‬‭);‬
‭Syntax:‬‭array.join(separator);‬ ‭Output‬‭:‬
‭newArray = [ 3 ];‬
‭ eturns‬‭: Joined elements as a string.‬
R ‭array = [ 1, 2,‬‭‘a’, ‘b’‬‭, 4, 5 ];‬
‭– Does not change the original array.‬

‭8.‬ ‭join()‬ ‭ eturns the first element in the array that satisfies a‬
R v‭ ar array = [ 1, 2, 3 ];‬
‭provided testing function.‬ ‭var joinedString = array.join( ‘-‘ );‬
‭Syntax‬‭: array.find(function(currentValue, index,‬ ‭Output‬‭: joinedString = “1-2-3”‬
‭arr),thisValue);‬

‭ eturns‬‭: The value of the first element that passes the‬


R
‭test.‬
‭Otherwise it returns undefined.‬

‭9.‬ ‭indexOf()‬ ‭ eturns the first index at which a given element is found in‬
R v‭ ar array = [ 1, 2,‬‭3‬‭, 4, 5 ];‬
‭the array.‬ ‭var index = array.indexOf( 3 );‬
‭Syntax‬‭: array.indexOf(item, start_position_for_search)‬ ‭Output:‬‭index = 2 // Index of 3‬
‭start_position_for_search = default 0;‬
v‭ ar index = array.indexOf( 3, 3 );‬
‭ eturns‬‭: index of search element.‬
R ‭Output:‬‭index = -1 // Index of 3 searched‬
‭Returns -1 if the value is not found.‬ ‭from index 3 hence not found.‬
‭– Does not change the original array.‬

‭10.‬ ‭ eturns the last index at which a given element is found in‬
R v‭ ar array = [ 1, 2, 3, 4, 5,‬‭3‬‭];‬
‭the array.‬ ‭(indexes –> 0, 1, 2, 3, 4, 5)‬
‭Syntax‬‭: array.lastIndexOf(item, start_position_for_search)‬ ‭var lastIndex = array.lastIndexOf( 3 );‬
‭– start_position_for_search = Default is the last element.‬ ‭Output‬‭: lastIndex = 5 // Last index of 3‬

‭ eturns‬‭: Last index of search element.‬


R
‭Returns -1 if the index is not found.‬
‭– Does not change the original array.‬

‭11.‬ ‭includes()‬ ‭ hecks if an array includes a certain element.‬


C v‭ ar array = [ 1, 2,‬‭3‬‭];‬
‭Syntax‬‭: array.includes(element, start_position_for_search);‬ ‭var includesThree = array.includes(‬‭3‬‭);‬
‭– start_position_for_search = default as 0.‬ ‭Output‬‭: includesThree = true;‬

‭ eturns‬‭: True if the value is found, otherwise false.‬


R
‭– Does not change the original array.‬

‭12.‬ ‭forEach()‬ ‭ xecutes a provided function once for each array element.‬
E v‭ ar array = [ 1, 2, 3 ];‬
‭Syntax‬‭: array.forEach(function(currentValue, index, arr),‬ ‭array.forEach(element =>‬
‭thisValue);‬ ‭console.log(element));‬
‭Output‬‭: 1, 2, 3‬
‭ eturns:‬‭undefined.‬
R
‭– Does not change the original array.‬

‭13.‬ ‭map()‬ ‭ reates a new array with the results of calling a provided‬
C v‭ ar array = [ 1, 2, 3 ];‬
‭function on every element in the array.‬ ‭var newArray = array.map(element =>‬
‭Syntax‬‭: array.map(function(currentValue, index, arr),‬ ‭element * 2);‬
‭thisValue);‬ ‭Output‬‭: newArray = [ 2, 4, 6 ];‬

‭ eturns‬‭: new array with updated results.‬


R
‭– Does not change the original array.‬

‭14.‬ ‭filter()‬ ‭ reates a new array with all elements that pass a test‬
C v‭ ar array = [ 1,‬‭2‭,‬ 3,‬‭4‬‭, 5 ];‬
‭implemented by the provided function.‬ ‭var filteredArray = array.filter(element =>‬
‭Syntax‬‭: array.filter(function(currentValue, index, arr),‬ ‭element % 2 === 0);‬
‭thisValue);‬ ‭Output‬‭: filteredArray = [‬‭2, 4‬‭];‬

‭ eturns‬‭: Array of elements that pass the test. Empty array‬


R v‭ ar filteredArray = array.filter(element =>‬
‭if no elements pass the test.‬ ‭element % 6 === 0);‬
‭– Does not change the original array.‬ ‭Output‬‭: filteredArray = [ ];‬

‭15.‬ ‭reduce()‬ ‭ pplies a function to reduce the array to a single value.‬


A v‭ ar array = [ 1, 2, 3, 4, 5 ];‬
‭Syntax‬‭: array.reduce(function(total, currentValue,‬ ‭var sum = array.reduce((acc, curr) => acc‬
‭currentIndex, arr), initialValue)‬ ‭+ curr, 0);‬
‭Output‬‭: sum = 15‬
‭ total =‬‭initialValue‬‭, or the previously returned value of the‬

‭function.‬
‭– currentValue = optional.‬
‭– currentIndex = optional.‬
‭– arr = optional.‬

‭ eturns‬‭: The accumulated result.‬


R
‭– Does not change the original array.‬

‭16.‬ ‭ imilar to reduce(), but processes the array from right to‬
S v‭ ar array = [ 1, 2, 3, 4, 5 ];‬
‭left.‬ ‭var result = array.reduceRight((acc, curr)‬
‭=> acc – curr);‬
‭ eturns‬‭: The accumulated result.‬
R ‭Output‬‭: result = -13‬
‭– Does not change the original array.‬

‭17.‬ ‭find()‬ ‭ eturns the index of the first element in the array that‬
R v‭ ar array = [ 1, 2, 3,‬‭4‬‭, 5 ];‬
‭satisfies a provided testing function.‬ ‭var foundElement = array.find(element =>‬
‭Syntax‬‭: array.findIndex(function(currentValue, index, arr),‬ ‭element > 3);‬
‭thisValue)‬ ‭Output‬‭: foundElement = 4‬

‭ eturns‬‭: The value of the first element that passes the test.‬
R
‭Otherwise it returns undefined.‬
‭– Does not change the original array.‬

‭18.‬ ‭ hecks if at least one element in the array satisfies a‬


C v‭ ar array = [ 1, 2, 3,‬‭4‬‭, 5 ];‬
‭provided testing function.‬ ‭var foundIndex = array.findIndex(element‬
‭Syntax‬‭: array.some(function(value, index, arr), this);‬ ‭=> element > 3);‬
‭Output‬‭: foundIndex = 3 // Index of 4‬
‭ eturns‬‭: true if any of the array elements pass the test,‬
R
‭otherwise false.‬
‭– Does not change the original array.‬

‭19.‬ ‭some()‬ ‭ hecks if at least one element in the array satisfies a‬


C v‭ ar array = [ 1, 2, 3, 4, 5 ];‬
‭provided testing function.‬ ‭var hasEvenNumber = array.some(element‬
‭Syntax‬‭: array.some(function(value, index, arr), this);‬ ‭=> element % 2 === 0);‬
‭Output‬‭: hasEvenNumber = true‬
‭ eturns‬‭: true if any of the array elements pass the test,‬
R
‭otherwise false.‬
‭– Does not change the original array.‬

‭20.‬ ‭every()‬ ‭ hecks if all elements in the array satisfy a provided testing‬
C v‭ ar array = [ 1, 2, 3, 4, 5 ];‬
‭function.‬ ‭var allPositive = array.every(element =>‬
‭Syntax‬‭: array.every(function(currentValue, index, arr),‬ ‭element > 0);‬
‭thisValue);‬ ‭Output‬‭: allPositive = true‬

‭ eturns‬‭: true if all elements pass the test, otherwise false.‬


R
‭– Does not change the original array.‬

‭21.‬ ‭sort()‬ ‭ orts the elements of an array in place.‬


S ‭ ar array = [ 3, 1, 4, 2, 5 ];‬
v
‭Syntax‬‭:‬‭array‬‭.sort(‬‭compareFunction‬‭);‬ ‭var sortedArray = array.sort();‬
‭Output‬‭:‬
‭ eturns‬‭: The array with the items sorted.‬
R ‭sortedArray = [1, 2, 3, 4, 5] // returned‬
‭– Overwrites the original array.‬ ‭array.‬
‭– Does not change the original array.‬ ‭array = [1, 2, 3, 4, 5] // Original array.‬

‭22.‬ ‭reverse‬ ‭ everses the order of the elements in an array.‬


R ‭ ar array = [ 1, 2, 3, 4, 5 ];‬
v
‭Syntax‬‭:‬‭array‬‭.reverse();‬ ‭var reversedArray = array.reverse();‬
‭Output‬‭:‬
‭ eturns‬‭: The reversed array.‬
R ‭reversedArray = [ 5, 4, 3, 2, 1 ] //‬
‭– Overwrites the original array.‬ ‭returned array.‬
‭array = [ 5, 4, 3, 2, 1 ] // original array.‬

‭23.‬ ‭isArray()‬ C
‭ hecks if a value is an array.‬ ‭ ar array = [ 1, 2, 3 ];‬
v
‭Syntax‬‭: Array.isArray‬‭(obj‬‭);‬ ‭var isArrayTrue = Array.isArray(array);‬

‭ eturns‬‭: true if the object is an array, otherwise false.‬


R ‭Output‬‭: isArrayTrue = true‬
‭– Does not change the original array.‬
‭24.‬ ‭length‬ ‭ roperty that represents the number of elements in an‬
P ‭ ar array = [ 1, 2, 3 ];‬
v
‭array.‬ ‭var length = array.length;‬
‭Syntax‬‭: array.length;‬
‭ utput‬‭: length = 3 // length of the‬
O
‭ eturns‬‭: The number of elements in the array.‬
R ‭array.‬
‭– Does not change the original array.‬

You might also like