Arrays are ordered list-like objects used to store multiple values in a single variable. In JavaScript, arrays are dynamic, zero-indexed, heterogeneous (can store mixed data types), and powered by prototype methods.
flowchart LR
Arr["Array: ['HTML', 'CSS', 'JS']"] --> Idx0["[0]: 'HTML'"]
Arr --> Idx1["[1]: 'CSS'"]
Arr --> Idx2["[2]: 'JS'"]
Arr --> Length["length: 3"]
| Property / Concept | Description | Example |
|---|---|---|
| Zero-Indexed | First element is at index 0, last element is at length - 1. |
arr[0] |
| Dynamic Length | Arrays resize automatically when elements are added or removed. | arr.length |
Array.isArray() |
Reliable runtime check to distinguish arrays from plain objects. | Array.isArray(arr) |
| Sparse Arrays | Arrays containing empty/uninitialized slots. | const arr = [1, , 3]; |
// Demonstrating Array creation, access, mutation, and inspection
// 1. Creating Arrays
const techStack = ["JavaScript", "TypeScript", "React", "Node.js"];
console.log(`Initial Array Length: ${techStack.length}`);
// 2. Accessing Elements
console.log(`First Element: ${techStack[0]}`);
console.log(`Last Element (at method): ${techStack.at(-1)}`); // ES2022 negative index access
// 3. Mutating Array Elements
techStack[2] = "Next.js"; // Replace "React" with "Next.js"
console.log("Updated Stack:", techStack);
// 4. Array.isArray Validation
console.log(`Is techStack an array? ${Array.isArray(techStack)}`); // true
console.log(`Is plain object an array? ${Array.isArray({ a: 1 })}`); // false
// 5. Destructuring Arrays
const [firstLanguage, secondLanguage, ...remainingStack] = techStack;
console.log(`Destructured First: ${firstLanguage}, Second: ${secondLanguage}`);
console.log("Remaining Stack:", remainingStack);
arr.at(-1) for Last Element: Modern ES2022 arr.at(-1) is cleaner than arr[arr.length - 1].arr[100] = "x", as it creates empty slots that degrade V8 optimization.Array.isArray(): typeof [] returns "object". Always use Array.isArray(val) to check if a value is an array.What is the value of arr.length after running const arr = [10, 20]; arr[5] = 60;?
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.