Previous Page : JavaScript Array

  1. .push(element1, element2, ...) : adds one or more elements to the end of an array and returns the new length of the array.

    let fruits = ['apple', 'banana'];
    console.log(fruits.push('mango')); // 3
    console.log(fruits); // ["apple", "banana", "mango"]
    
  2. .pop() : removes the last element of an array and returns that element.

    let fruits = ['apple', 'banana', 'mango'];
    console.log(fruits.pop()); // "mango"
    console.log(fruits); // ["apple", "banana"]
    
  3. .shift() : removes the first element of an array and returns that element

    let fruits = ['apple', 'banana', 'mango'];
    console.log(fruits.shift()); // "apple"
    console.log(fruits); // ["banana", "mango"]
    
  4. .unshift(element1, element2, ...) : adds one or more elements to the beginning of an array and returns the new length of the array

    let fruits = ['banana', 'mango'];
    console.log(fruits.unshift('apple')); // 3
    console.log(fruits); // ["apple", "banana", "mango"]
    
  5. .concat(array2, array3, ...) : combines two or more arrays and returns a new array

    let fruits1 = ['apple', 'banana'];
    let fruits2 = ['mango', 'orange'];
    let fruits3 = fruits1.concat(fruits2);
    console.log(fruits3); // ["apple", "banana", "mango", "orange"]
    
  6. .slice(start, end) : returns a new array that includes elements from the original array, between the start and end indices (end is not included)

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    console.log(fruits.slice(1, 3)); // ["banana", "mango"]
    console.log(fruits); // ["apple", "banana", "mango", "orange"] (original array remains unchanged)
    
  7. .splice(start, deleteCount, element1, element2, ...) : modifies an array by removing elements, and/or adding new elements, starting at the specified index

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    fruits.splice(1, 2, 'kiwi');
    console.log(fruits); // ["apple", "kiwi", "orange"]
    
  8. .indexOf(element) : returns the first index at which a given element can be found in the array, or -1 if it is not present

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    console.log(fruits.indexOf('mango')); // 2
    console.log(fruits.indexOf('pineapple')); // -1
    
  9. .lastIndexOf(element) : returns the last index at which a given element can be found in the array, or -1 if it is not present.

    let fruits = ['apple', 'banana', 'mango', 'orange', 'banana'];
    console.log(fruits.lastIndexOf('banana')); // 4
    console.log(fruits.lastIndexOf('pineapple')); // -1
    
  10. .forEach(function(currentValue, index, array) { ... }) : calls a provided function once for each array element.

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    fruits.forEach(function(fruit, index) {
      console.log(fruit, index);
    });
    /* Output:
    apple 0
    banana 1
    mango 2
    orange 3
    */
    
  11. .map(function(currentValue, index, array) { ... }) : creates a new array with the results of calling a provided function on every element in this array.

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    let upperCaseFruits = fruits.map(function(fruit) {
      return fruit.toUpperCase();
    });
    console.log(upperCaseFruits); // ["APPLE", "BANANA", "MANGO", "ORANGE"]
    
  12. .filter(function(currentValue, index, array) { ... }) : creates a new array with all elements that pass the test implemented by the provided function.

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    let longFruits = fruits.filter(function(fruit) {
      return fruit.length > 5;
    });
    console.log(longFruits); // ["banana"]
    
  13. .reduce(function(accumulator, currentValue, currentIndex, array) { ... }, initialValue) : applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

    let fruits = ['apple', 'banana', 'mango', 'orange'];
    let concatenatedFruits = fruits.reduce(function(prev, current) {
      return prev + ', ' + current;
    });
    console.log(concatenatedFruits); // "apple, banana, mango, orange"
    

    These are some of the most commonly used array methods in JavaScript.

    JOIN WHATSAPP COMMUNITY FOR MORE SUCH AWESOME CONTENT :

    <aside> 💡 CLICK HERE TO JOIN

    </aside>

    Join Instagram Community ( 80k+) : @webdevarmy & @cssdevarmy

    Youtube : @webdevarmy