Some of the array functions (methods) in JavaScript are:
push()
: Adds one or more elements to the end of an array and returns the new length of the array.
let fruits = ['apple', 'banana']; fruits.push('orange'); console.log(fruits); // Output: ['apple', 'banana', 'orange']
pop()
: Removes the last element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange']; let lastFruit = fruits.pop(); console.log(lastFruit); // Output: 'orange' console.log(fruits); // Output: ['apple', 'banana']
shift()
: Removes the first element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange']; let firstFruit = fruits.shift(); console.log(firstFruit); // Output: 'apple' console.log(fruits); // Output: ['banana', 'orange']
unshift()
: Adds one or more elements to the beginning of an array and returns the new length of the array.
let fruits = ['banana', 'orange']; fruits.unshift('apple'); console.log(fruits); // Output: ['apple', 'banana', 'orange']
concat()
: Combines two or more arrays and returns a new array.
let fruits = ['apple', 'banana']; let moreFruits = ['orange', 'mango']; let allFruits = fruits.concat(moreFruits); console.log(allFruits); // Output: ['apple', 'banana', 'orange', 'mango']
slice()
: Returns a shallow copy of a portion of an array into a new array.
let fruits = ['apple', 'banana', 'orange', 'mango']; let citrusFruits = fruits.slice(2); console.log(citrusFruits); // Output: ['orange', 'mango']
splice()
: Changes the contents of an array by removing, replacing, or adding elements.
let fruits = ['apple', 'banana', 'orange', 'mango']; fruits.splice(1, 2, 'kiwi', 'grape'); console.log(fruits); // Output: ['apple', 'kiwi', 'grape', 'mango']
forEach()
: Executes a provided function once for each array element.
let fruits = ['apple', 'banana', 'orange']; fruits.forEach(function(fruit) { console.log(fruit); }); // Output: // 'apple' // 'banana' // 'orange'
These are just a few examples of the many array functions available in JavaScript.