Array Methods
Array Methods
Searching Arrays
let fruits = ["apple", "banana", "cherry"];
fruits.includes("banana"); // true — is it there?
fruits.indexOf("cherry"); // 2 — at what position?
fruits.indexOf("mango"); // -1 — not found
fruits.find(f => f.length > 5); // "banana" — first match
fruits.findIndex(f => f.length > 5); // 1 — its index
includes() answers "is this value in the array?" with a simple true/false. indexOf() returns the position, or -1 if absent — that -1 convention again. Two newer methods take a FUNCTION instead of a value: find() returns the first item matching a condition, and findIndex() returns its position. These are your first array methods that accept a function, a pattern that dominates the next chapter.slice() — copy a portion (does NOT modify)
let nums = [10, 20, 30, 40, 50];
nums.slice(1, 3); // [20, 30] — from index 1 up to (not incl.) 3
nums.slice(2); // [30, 40, 50] — from index 2 to the end
nums.slice(-2); // [40, 50] — last two items
console.log(nums); // [10,20,30,40,50] ← ORIGINAL UNCHANGED
let copy = nums.slice(); // a quick way to copy an entire array
slice() returns a NEW array containing a copied portion — the original is untouched. The rules are identical to string slice: the start index is included, the end index is excluded, and negative numbers count from the end. Two everyday uses: grabbing a subset of items, and — with no arguments at all — making a complete copy of an array. That copy trick matters because assigning an array to another variable does NOT copy it (both names then point to the same array).splice() — add or remove (DOES modify)
let items = ["a", "b", "c", "d"];
// splice(startIndex, howManyToDelete, ...itemsToAdd)
items.splice(1, 2); // remove 2 items from index 1
console.log(items); // ["a", "d"] ← ORIGINAL CHANGED
let colors = ["red", "blue"];
colors.splice(1, 0, "green"); // remove 0, insert "green" at index 1
console.log(colors); // ["red", "green", "blue"]
splice() changes the original array — it can remove items, insert items, or both. Its first argument is where to start, the second is how many to delete, and any further arguments are inserted at that spot. Pass 0 as the delete count to insert without removing anything. Don't confuse slice and splice, a classic exam question: slice coPIES (safe), splice modifies (destructive). When in doubt, remember that only the one with the extra "p" changes the original.concat, join, and reverse
let a = [1, 2], b = [3, 4];
a.concat(b); // [1,2,3,4] — joins arrays into a NEW array
[...a, ...b]; // [1,2,3,4] — the modern spread way
["Aman","Priya"].join(", "); // "Aman, Priya" — array → string
[1,2,3].reverse(); // [3,2,1] ← MODIFIES the original!
concat() merges arrays into a new one, leaving the originals alone. Modern code often prefers the spread operator [...a, ...b], which you'll meet properly later — same result, cleaner syntax. join() turns an array into a string with a separator you choose (the reverse of split() from the strings chapter). And reverse() flips the order — but note it modifies the original array in place, so slice().reverse() is safer if you want to keep the original.
Sorting Arrays — the number gotcha
let names = ["Ravi", "Aman", "Priya"];
names.sort(); // ["Aman","Priya","Ravi"] ✓ alphabetical
let nums = [10, 5, 100, 1];
nums.sort(); // [1, 10, 100, 5] ✗ WRONG! Sorted as TEXT
// The fix — provide a compare function:
nums.sort((a, b) => a - b); // [1, 5, 10, 100] ✓ ascending
nums.sort((a, b) => b - a); // [100, 10, 5, 1] ✓ descending
sort() converts everything to STRINGS and sorts alphabetically — which works fine for names but produces nonsense for numbers (100 comes before 5 because "1" comes before "5"). This surprises everyone once. The fix is a compare function: sort((a, b) => a - b) for ascending, (a, b) => b - a for descending. Memorise those two lines — you'll need them constantly. Also note sort() modifies the original array, so copy first with slice() if you need the original order preserved.Exam Corner
Q: How do you copy an entire array?
arr.slice() or [...arr].Q: Why does [10,5,100].sort() give the wrong order? Because sort converts items to strings and compares alphabetically.
Q: How do you sort numbers ascending?
arr.sort((a, b) => a - b)Q: What does join(", ") do? Converts an array into a string, separating items with ", ".
Arrays में खोजना
let fruits = ["apple", "banana", "cherry"];
fruits.includes("banana"); // true — kya yeh hai?
fruits.indexOf("cherry"); // 2 — kis position par?
fruits.indexOf("mango"); // -1 — nahi mila
fruits.find(f => f.length > 5); // "banana" — pehla match
fruits.findIndex(f => f.length > 5); // 1 — uska index
includes() "क्या यह value array में है?" का जवाब सरल true/false से देता है. indexOf() position लौटाता है, या न होने पर -1 — फिर वही -1 convention. दो नए methods value के बजाय FUNCTION लेते हैं: find() condition से मिलता पहला item लौटाता है, और findIndex() उसकी position. ये आपके पहले array methods हैं जो function स्वीकार करते हैं, वह pattern जो अगले chapter पर हावी है.slice() — हिस्सा copy (बदलता NAHI)
let nums = [10, 20, 30, 40, 50];
nums.slice(1, 3); // [20, 30] — index 1 se 3 tak (3 shamil nahi)
nums.slice(2); // [30, 40, 50] — index 2 se ant tak
nums.slice(-2); // [40, 50] — aakhri do items
console.log(nums); // [10,20,30,40,50] ← ORIGINAL ANBADLA
let copy = nums.slice(); // poori array copy karne ka jaldi tarika
slice() NAYI array लौटाता है जिसमें copy किया हिस्सा है — मूल अछूता रहता है. Rules string slice जैसे ही हैं: start index शामिल, end index बाहर, और negative numbers अंत से गिनते हैं. दो रोज़ के इस्तेमाल: items का subset पकड़ना, और — बिना किसी argument के — array की पूरी copy बनाना. वह copy trick मायने रखती है क्योंकि array को दूसरे variable में assign करना उसे copy NAHI करता (तब दोनों नाम वही array point करते हैं).splice() — जोड़ना या हटाना (बदलता HAI)
let items = ["a", "b", "c", "d"];
// splice(startIndex, kitne delete karne, ...items jodne ko)
items.splice(1, 2); // index 1 se 2 items hatao
console.log(items); // ["a", "d"] ← ORIGINAL BADLA
let colors = ["red", "blue"];
colors.splice(1, 0, "green"); // 0 hatao, index 1 par "green" daalo
console.log(colors); // ["red", "green", "blue"]
splice() मूल array बदलता है — यह items हटा सकता है, डाल सकता है, या दोनों. इसका पहला argument कहां से शुरू करना, दूसरा कितने delete करने, और आगे के arguments उस जगह डाले जाते हैं. बिना कुछ हटाए डालने को delete count में 0 भेजिए. slice और splice मत उलझाइए, classic exam सवाल: slice coPIES (सुरक्षित), splice बदलता है (विनाशकारी). संदेह हो तो याद रखिए सिर्फ अतिरिक्त "p" वाला मूल बदलता है.concat, join, और reverse
let a = [1, 2], b = [3, 4];
a.concat(b); // [1,2,3,4] — arrays ko NAYI array me jodta
[...a, ...b]; // [1,2,3,4] — modern spread tarika
["Aman","Priya"].join(", "); // "Aman, Priya" — array → string
[1,2,3].reverse(); // [3,2,1] ← ORIGINAL BADALTA hai!
concat() arrays को नई array में मिलाता है, मूल को अकेला छोड़ते. Modern code अक्सर spread operator [...a, ...b] पसंद करता है, जिसे आप ठीक से बाद में मिलेंगे — वही नतीजा, साफ syntax. join() array को आपके चुने separator वाली string में बदलता है (strings chapter के split() का उल्टा). और reverse() order पलटता है — पर ध्यान यह मूल array को जगह पर ही बदलता है, तो मूल रखना हो तो slice().reverse() सुरक्षित है.
Arrays Sort करना — number का जाल
let names = ["Ravi", "Aman", "Priya"];
names.sort(); // ["Aman","Priya","Ravi"] ✓ varnkram
let nums = [10, 5, 100, 1];
nums.sort(); // [1, 10, 100, 5] ✗ GALAT! TEXT ki tarah sort
// Hal — compare function do:
nums.sort((a, b) => a - b); // [1, 5, 10, 100] ✓ ascending
nums.sort((a, b) => b - a); // [100, 10, 5, 1] ✓ descending
sort() हर चीज़ को STRINGS में बदलकर वर्णक्रम से sort करता है — जो नामों के लिए ठीक चलता है पर numbers के लिए बकवास देता है (100 5 से पहले आता है क्योंकि "1" "5" से पहले आता है). यह सबको एक बार चौंकाता है. हल compare function है: ascending के लिए sort((a, b) => a - b), descending के लिए (a, b) => b - a. वे दो lines याद कीजिए — आपको लगातार चाहिए होंगी. यह भी ध्यान sort() मूल array बदलता है, तो मूल order रखना हो तो पहले slice() से copy कीजिए.Exam Corner
Q: पूरी array कैसे copy करते हैं?
arr.slice() या [...arr].Q: [10,5,100].sort() गलत order क्यों देता है? क्योंकि sort items को strings में बदलकर वर्णक्रम से तुलना करता है.
Q: Numbers को ascending कैसे sort करते हैं?
arr.sort((a, b) => a - b)Q: join(", ") क्या करता है? Array को string में बदलता है, items को ", " से अलग करते.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.