๐ JavaScript Array Methods & Their Explanations
๐ 1. push()
Adds one or more elements to the end of an array and returns the new length.
let arr = [1, 2];
arr.push(3); // [1, 2, 3]
---------------------------------
๐ 2. pop()
Removes the last element from an array and returns it.
let arr = [1, 2, 3];
arr.pop(); // 3 (arr becomes [1, 2])
---------------------------------
๐ 3. unshift()
Adds elements to the beginning of an array and returns the new length.
let arr = [2, 3];
arr.unshift(1); // [1, 2, 3]
---------------------------------
๐ฝ 4. shift()
Removes the first element from an array and returns it.
let arr = [1, 2, 3];
arr.shift(); // 1 (arr becomes [2, 3])
---------------------------------
๐ 5. indexOf()
Finds the first index of a specified element, or returns -1 if not found.
let arr = [10, 20, 30, 40];
arr.indexOf(30); // 2
---------------------------------
๐ 6. reverse()
Reverses the order of elements in place.
let arr = [1, 2, 3];
arr.reverse(); // [3, 2, 1]
---------------------------------
๐๏ธ 7. concat()
Merges two or more arrays without modifying the original arrays.
let arr1 = [1, 2];
let arr2 = [3, 4];
let result = arr1.concat(arr2); // [1, 2, 3, 4]
---------------------------------
โ๏ธ 8. slice()
Returns a shallow copy of a portion of an array.
let arr = [10, 20, 30, 40];
arr.slice(1, 3); // [20, 30]
---------------------------------
๐ ๏ธ 9. splice()
Adds/removes elements from an array at a specific index.
let arr = [1, 2, 3, 4];
arr.splice(1, 2, 99); // Removes 2 and 3, adds 99 โ [1, 99, 4]
---------------------------------
๐ญ 10. map()
Creates a new array by applying a function to each element.
let arr = [1, 2, 3];
let doubled =
arr.map(x => x * 2); // [2, 4, 6]
---------------------------------
๐ฏ 11. filter()
Creates a new array with elements that pass a test.
let arr = [10, 20, 30, 40];
let filtered = arr.filter(x => x > 20); // [30, 40]
---------------------------------
๐ข 12. reduce()
Reduces the array to a single value by applying a function.
let arr = [1, 2, 3, 4];
let sum = arr.reduce((acc, curr) => acc curr, 0); // 10
---------------------------------
๐ 13. find()
Returns the first element that passes a test.
let arr = [5, 10, 15];
let found = arr.find(x => x > 6); // 10
---------------------------------
๐ข 14. findIndex()
Returns the index of the first element that matches a condition.
let arr = [3, 6, 9];
arr.findIndex(x => x > 5); // 1
---------------------------------
โ 15. every()
Checks if all elements pass a test, returns true or false.
let arr = [2, 4, 6];
arr.every(x => x % 2 === 0); // true
---------------------------------
โ
16. some()
Checks if at least one element passes a test.
let arr = [1, 3, 5, 8];
arr.some(x => x % 2 === 0); // true
---------------------------------
๐ค 17. join()
Joins array elements into a string using a separator.
let arr = ['Hello', 'World'];
arr.join(' '); // "Hello World"
---------------------------------
๐ 18. sort()
Sorts an array in place (default: lexicographical).
let arr = [40, 100, 1, 5];
arr.sort((a, b) => a - b); // [1, 5, 40, 100]
---------------------------------
๐ 19. forEach()
Executes a function for each array element.
let arr = [1, 2, 3];
arr.forEach(x => console.log(x * 2)); // 2, 4, 6
---------------------------------
๐ 20. flat()
Flattens nested arrays into a single-level array.
let arr = [1, [2, 3], [4, 5]];
arr.flat(); // [1, 2, 3, 4, 5]
---------------------------------
#JavaScript #ArrayMethods #LearnJavaScript #WebDevelopment #JSArrays #coder #coding #FrontendDevelopment #fullstack #react #developer
#JavaScriptTutorial #CodingForBeginners #JSForBeginners #JavaScriptTips #WebDevTips #CodeWithMe #tips #ai #ui #ux #designer
#JSMapMethod #JSFilterMethod #JSReduceMethod #JSSliceMethod #JSArrayTricks