JS Array Methods
In this page:
array.push(item);
array.pop();
array.map(item => expression);
array.filter(item => condition);
array.slice(start, end);
Adding and Removing: push, pop, shift, unshift
push() adds an element to the end of an array, pop() removes the last element, unshift() adds an element to the beginning, and shift() removes the first element, all modifying the original array directly.
उदाहरण: Adding and Removing: push, pop, shift, unshift
const list = [2, 3];
list.push(4);
list.unshift(1);
console.log(list); // [1, 2, 3, 4]
list.pop();
list.shift();
console.log(list); // [2, 3]
splice, slice, concat, and join
splice() adds or removes elements at any position and modifies the array in place, slice() extracts a portion without modifying the original, concat() combines arrays into a new one, and join() converts an array into a single string.
उदाहरण: splice, slice, concat, and join
const nums = [1, 2, 3, 4];
console.log(nums.slice(1, 3)); // [2, 3], original untouched
nums.splice(1, 1, "x");
console.log(nums); // [1, "x", 3, 4]
console.log([1, 2].concat([3, 4]));
console.log(nums.join("-"));
indexOf and find
indexOf() returns the position of the first matching element or -1 if it's not found, while find() returns the first element that satisfies a given test function, or undefined if none match.
उदाहरण: indexOf and find
// Declare the constant `nums`, set to `[10, 20, 30]`
// Declare the constant `nums`, set to `[10, 20, 30]`
const nums = [10, 20, 30];
// Print `nums.indexOf(20)` to the console
// Print `nums.indexOf(20)` to the console
console.log(nums.indexOf(20));
// Print `nums.find(n => n > 15)` to the console
// Print `nums.find(n => n > 15)` to the console
console.log(nums.find(n => n > 15));
filter and map
filter() creates a new array containing only elements that pass a given test, while map() creates a new array where every element has been transformed by a given function, both leave the original array untouched.
उदाहरण: filter and map
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
const nums = [1, 2, 3, 4, 5];
// Print `nums.filter(n => n % 2 === 0)` to the console
// Print `nums.filter(n => n % 2 === 0)` to the console
console.log(nums.filter(n => n % 2 === 0));
// Print `nums.map(n => n * 2)` to the console
// Print `nums.map(n => n * 2)` to the console
console.log(nums.map(n => n * 2));
reduce, forEach, and sort
reduce() combines every element of an array into a single accumulated value, forEach() runs a function once for each element purely for side effects without returning anything useful, and sort() reorders an array's elements, by default treating them as strings unless given a custom compare function.
उदाहरण: reduce, forEach, and sort
// Declare the constant `nums`, set to `[4, 2, 7]`
// Declare the constant `nums`, set to `[4, 2, 7]`
const nums = [4, 2, 7];
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
console.log(nums.reduce((sum, n) => sum + n, 0));
// Call `nums.forEach(n => console.log(n))`
// Call `nums.forEach(n => console.log(n))`
nums.forEach(n => console.log(n));
// Print `[...nums].sort((a, b) => a - b)` to the console
// Print `[...nums].sort((a, b) => a - b)` to the console
console.log([...nums].sort((a, b) => a - b));
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: