Basics
Arrays
Ordered Collections
💡 Array ek train jaisa hai — bogies (elements) ek order mein, har ek ka apna index (position number, 0 se shuru). Items add/remove kar sakte ho, order maintain rehta hai.
Array [] se banta hai — DIFFERENT types ki values rakh sakta hai ek saath, dynamically resizable. Common methods: push() (end mein add), pop() (end se remove), length (size), indexing (arr[0]).
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple"
console.log(fruits.length); // 3
fruits.push("date"); // end mein add
console.log(fruits); // ['apple', 'banana', 'cherry', 'date']
fruits.pop(); // end se remove
fruits.unshift("mango"); // START mein add
fruits.shift(); // START se remove
const mixed = [1, "two", true, null]; // different types ek saath📋
Array ek train jaisa hai — bogies (elements) ek order mein, har ek ka apna index (position number, 0 se shuru). Items add/remove kar sakte ho, order maintain rehta hai.
1 / 5
⚡ Quick Recap
- [] se array banta hai, index 0 se shuru
- push/pop = end se add/remove, unshift/shift = start se
- Array.isArray() = reliable array check (typeof unreliable hai)
On this page (2 subtopics)
Arrays se values seedha variables mein "unpack" kar sakte ho — [a, b] = array — bahut concise, aage ES6+ Features category mein detail se aayega.
const [first, second] = ["Aarav", "Riya"];
console.log(first); // "Aarav"
console.log(second); // "Riya"
const [a, , c] = [1, 2, 3]; // comma se skip kar sakte ho
console.log(a, c); // 1 3Tip: Array destructuring especially function returns mein bahut useful hai — jaise const [value, setValue] = useState() (React ka famous pattern).
Arrays ke andar arrays — 2D grids/matrices represent karne ke liye, jaise tic-tac-toe board ya game grid.
const grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(grid[1][2]); // 6 — row 1, column 2
for (const row of grid) {
for (const cell of row) {
console.log(cell);
}
}Tip: JavaScript arrays "jagged" ho sakte hain — har row ki length alag ho sakti hai (true 2D array languages jaise C ke ulat, jaha fixed rectangular shape zaroori hai).