Arrays
Arrays in programming are data structures used to store collections of items of the same type. In JavaScript, an array is a special type of object that stores a list of values (elements) indexed by a numeric index, starting from 0.
// Creating an array
let fruits = ['apple', 'banana', 'orange'];
// Accessing elements by index
console.log(fruits[0]); // Output: 'apple'
console.log(fruits[1]); // Output: 'banana'
// Adding elements
fruits.push('grape'); // Adds 'grape' to the end of the array
console.log(fruits); // Output: ['apple', 'banana', 'orange', 'grape']
// Removing elements
let removedFruit = fruits.pop(); // Removes the last element ('grape')
console.log(removedFruit); // Output: 'grape'
console.log(fruits); // Output: ['apple', 'banana', 'orange']
// Iterating over elements
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// Output:
// 'apple'
// 'banana'
// 'orange'
Array Methods (most commonly used - in detail)
JavaScript array methods are built-in functions that operate on arrays to perform various operations such as adding, removing, or modifying elements.
These methods help developers efficiently manipulate arrays and perform common tasks.
Here's an overview of some commonly used JavaScript array methods:
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'); // ['apple', 'banana', 'orange']
pop()
Removes the last element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange'];
fruits.pop(); // ['apple', 'banana']
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'); // ['apple', 'banana', 'orange']
shift()
Removes the first element from an array and returns that element.
let fruits = ['apple', 'banana', 'orange'];
fruits.shift(); // ['banana', 'orange']
splice()
array.splice(startIndex, deleteCount, item1, item2, ...);
Adds or removes elements from an array at a specified index, modifies the original array and returns an array containing the removed elements, if any.
startIndex
: The index at which to start adding or removing elements.deleteCount
: The number of elements to remove from the array. If set to 0, no elements are removed.item1, item2, ...
: Elements to add to the array, starting at the startIndex.
Removing
elements with splice()
let fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
let removedFruits = fruits.splice(1, 2); // Removes 2 elements starting from index 1
console.log(fruits); // Output: ['apple', 'grape', 'kiwi']
console.log(removedFruits); // Output: ['banana', 'orange']
Adding
elements with splice()
let fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 0, 'grape', 'kiwi'); // Inserts 'grape' and 'kiwi' at index 1
console.log(fruits); // Output: ['apple', 'grape', 'kiwi', 'banana', 'orange']
Replacing
elements with splice()
let fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 1, 'grape', 'kiwi'); // ['apple', 'grape', 'kiwi', 'orange']
slice ()
array.slice(start, end);
Returns a shallow copy of a portion of an array into a new array object.
-
start
: The index at which to begin extraction. If negative, it indicates an offset from the end of the array. -
end
: The index before which to end extraction. The extracted slice will not include the element at this index. If end is not specified, slice() extracts to the end of the array. If negative, it indicates an offset from the end of the array.
let fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
// Extract elements from index 1 to index 3 (not including index 3)
let citrus = fruits.slice(1, 3);
console.log(citrus); // Output: ['banana', 'orange']
// Extract elements from index 2 to the end of the array
let remainingFruits = fruits.slice(2);
console.log(remainingFruits); // Output: ['orange', 'grape', 'kiwi']
// Extract the last two elements of the array
let lastTwo = fruits.slice(-2);
console.log(lastTwo); // Output: ['grape', 'kiwi']
// Extract elements from index -3 to index -1
let recentFruits = fruits.slice(-3, -1);
console.log(recentFruits); // Output: ['orange', 'grape']
concat()
Returns a new array that combines the elements of the original array with other arrays or values.
let fruits = ['apple', 'banana'];
let moreFruits = ['orange', 'grape'];
let allFruits = fruits.concat(moreFruits); // ['apple', 'banana', 'orange', 'grape']
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
some()
array.some(callback(element, index, array), thisArg);
Tests whether at least one element
in the array passes the test implemented by the provided function, returns a boolean value.
True
if at least one element in the array satisfies the conditionFalse
if not even one element in the array satisfies the condition
// Example
const numbers = [1, 3, 5, 7, 8, 9, 11];
const hasEvenNumber = numbers.some((num) => num % 2 === 0);
console.log(hasEvenNumber); // Output: true (because 8 is an even number)
filter()
array.filter(callback(element, index, array), thisArg);
Tests whether all the elements pass the test implemented by the provided function, create a new array with elements that passed, returns the new array.
const temperatures = [20, 25, 28, 22, 30, 18];
const highTemperatures = temperatures.filter((temp) => temp > 25);
console.log(highTemperatures); // Output: [28, 30]