JavaScript array method every developer should know!

  • push()
  • pop()
  • shift()
  • unshift()
  • concat()
  • slice()
  • splice()
  • reverse()
  • every()
  • join()
  • forEach()
  • map()
  • filter()
  • find()
  • reduce()
  • sort()
let products = ['chairs', 'tables', 'binders', 'paper', 'copiers'];
newProducts = products.push('phones');
console.log(newProducts);
// Output: 6
console.log(products);
// Output: [ 'chairs', 'tables', 'binders', 'paper', 'copiers', 'phones' ]
let products = ['chairs', 'tables', 'binders', 'paper', 'copiers'];
popProduct = products.pop();
console.log(popProduct);
// Output: copiers
console.log(products);
// Output: [ 'chairs', 'tables', 'binders', 'paper' ]
let products = ['chairs', 'tables', 'binders', 'paper', 'copiers'];
shiftProduct = products.shift();
console.log(shiftProduct);
// Output: chairs
console.log(products);
// Output: [ 'tables', 'binders', 'paper', 'copiers' ]
let products = ['chairs', 'tables', 'binders', 'paper', 'copiers'];
unshiftProduct = products.unshift('phones');
console.log(unshiftProduct);
// Output: 6
console.log(products);
// Output: [ 'phones', 'chairs', 'tables', 'binders', 'paper', 'copiers' ]
let arr1 = [12, 15, 34, 56, 96];
let arr2 = [09, 20, 45, 89, 90];
newArr = arr1.concat(arr2);
console.log(newArr);
// Output: [ 12, 15, 34, 56, 96, 9, 20, 45, 89, 90 ]
let friends = ['Abby', 'Smith', 'Bob', 'Robert', 'Jane', 'Anna'];
let sliceFriends1 = friends.slice(2); /*start from position 2 to end*/
console.log(sliceFriends1); /*start from position 2 to end*/
// Output: [ 'Bob', 'Robert', 'Jane', 'Anna' ]
sliceFriends2 = friends.slice(2, 4); /*start from position 2 to 3, excluding 4*/
console.log(sliceFriends2);
// Output: [ 'Bob', 'Robert' ]
let sliceFriends3 = friends.slice(2, 5);
/*start from position 2 to 4, excluding 5*/
console.log(sliceFriends2);
// Output: [ 'Bob', 'Robert', 'Jane' ]
let days = ['Saturday', 'Monday', 'Tuesday', 'Thursday'];/*starting index number, ending index number, will remove item(s) based on this numbers*/
days.splice(1, 2);
console.log(days);
// Output: [ 'Saturday', 'Thursday' ]
days.splice(1);
console.log(days);
// Output: [ 'Saturday' ]
/*Insert at index 1 */
let days2 = ['Saturday', 'Monday', 'Tuesday', 'Thursday'];
days2.splice(1, 0, 'Sunday');
console.log(days2);
// Output: [ 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Thursday' ]
/*Insert at index 4 */
days2.splice(4, 0, 'Wednesday');
console.log(days2);
// Output: [ 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday' ]
/*replaces 1 element at index 4 */
let days3 = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Thursday'];
days3.splice(4, 1, 'Wednesday');
console.log(days3);
// Output: [ 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday' ]
let array1 = ['Sat', 'Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri'];
let reversed = array1.reverse();
console.log(reversed);
// Output: [ 'Fri', 'Thur', 'Wed', 'Tues', 'Mon', 'Sun', 'Sat' ]
let dob = ['day', 'month', 'year'];
console.log(dob.join('/'));
// Output: day/month/year
console.log(dob.join(' '));
// Output: day month year
let array1 = [45, 56, 8, 9, 10];
let checkArray1 = array1.every((item) => item > 7);
console.log(checkArray1);
// Output: true
let array2 = [5, 6, 23, 10];
let checkArray2 = array2.every((item) => item > 7);
console.log(checkArray2);
// Output: false
let array = ['Sat', 'Sun', 'Mon'];
array.forEach((item) => console.log(item));
// Output:
// Sat
// Sun
// Mon
let prices = [100, 80, 35, 99, 50, 70, 88];
let doublePrices = prices.map((price) => price * 2);
console.log(doublePrices);
// Output: [ 200, 160, 70, 198, 100, 140, 176 ]
let prices = [100, 80, 35, 99, 50, 70, 88];
let newPrices = prices.filter((price) => price > 50);
console.log(newPrices);
// Output: [ 100, 80, 99, 70, 88 ]
let prices = [100, 80, 35, 99, 50, 70, 88];
let newPrices = prices.find((price) => price > 50);
console.log(newPrices);
// Output: 100
// Adding values- classic example
let prices = [100, 80, 35, 99, 50, 70, 88];
let totalPrice = 0;
for (let i = 0; i < prices.length; i++) {
totalPrice += prices[i];
}
console.log(totalPrice);
// Output: 522
// Using reduce method:
let totalReduce = prices.reduce((total, price) => total + price, 0);
console.log(totalReduce);
// Output: 522
let prices = [100, 80, 35, 99, 50, 70, 88];
// ascending sorting:
let sortedPrice = prices.sort((a, b) => a - b);
console.log(sortedPrice);
// Output : [ 35, 50, 70, 80, 88, 99, 100 ]
// descending sorting:
let sortedPriceDesc = prices.sort((a, b) => b - a);
console.log(sortedPriceDesc);
// Output : [ 100, 99, 88, 80, 70, 50, 35 ]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store