Filter
Exclude
Time range
-
Near
🚀 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
25
5
32
1,161