JavaScript Notes
Learn JavaScript array basics from scratch: creation, indexing, length, iteration, mutation vs copy, and real-world patterns. 30+ examples with outputs.
Arrays are the backbone of JavaScript programming. Whether you're building a to-do list, a product catalog, or a data dashboard — arrays are everywhere. This guide walks you through every fundamental concept with clear diagrams, practical examples, and interview-ready answers.
Creating Arrays
1. Array Literal (✅ Recommended)
[]
0
[95, 87, 76, 100]
["Delhi", "Mumbai", "Bangalore"]
[42, "hello", true, null, {name: "JS"}]2. Array Constructor
[empty × 3] 3 undefined [1, 2, 3]
3. Array.from()
Converts iterables (strings, Sets, NodeLists) into arrays:
["H","e","l","l","o"] [1, 2, 3] [1, 4, 9, 16, 25]
4. Array.of()
Fixes the single-number gotcha of new Array():
[5] [1, 2, 3]
5. Spread Operator
[1, 2, 3] [1, 2, 3, 4]
Accessing Array Elements
Standard Index Access
"Red" "Blue" "Yellow" undefined
Array.at() — ES2022 (Supports Negative Index)
10 50 40
Array Length Property
4 ["a", "b"] ["a", "b", empty × 3] undefined
Key Rule:lengthis always one more than the highest index.arr.length - 1gives you the last index.
Modifying Array Elements
[1, 2, 99, 4, 5] [1, 2, 99, 4, 5, empty × 2, 100] 8 [1, 2, empty, 4, 5, empty × 2, 100]
Iterating Arrays
for Loop (Classic)
0 Apple 1 Banana 2 Cherry
for...of Loop (Modern — Recommended)
Apple Banana Cherry
forEach()
Item 1: ₹100 Item 2: ₹200 Item 3: ₹300
entries() — Index + Value
0: HTML 1: CSS 2: JS
Multidimensional Arrays
6 1 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9
Arrays are Reference Types
This is one of the most important — and most misunderstood — concepts in JavaScript:
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4, 99]
Real World Use Cases
Cart has 4 items Passing: [78, 92, 88, 95] Average: 83.6 ["Burger", "Pizza", "Fries"]
Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
const arr = new Array(5) when you want [5] | Array.of(5) or [5] | Constructor treats single number as length |
const b = a for a copy | const b = [...a] | Assignment copies the reference, not the data |
arr[arr.length] for last | arr[arr.length - 1] or arr.at(-1) | Length is 1 more than last index |
delete arr[i] to remove | arr.splice(i, 1) | delete leaves a hole |
| Accessing index on empty array | Always check arr.length > 0 first | Returns undefined, not an error |
Cheat Sheet
Interview Questions
Q1. What is an array in JavaScript and how does it differ from an object?
An array is an ordered, indexed collection of values. Objects use string keys; arrays use numeric indices starting from 0. Internally, arrays are objects — typeof [] === "object" — but Array.isArray([]) returns true.
Q2. What is the difference between const a = arr and const a = [...arr]?
const a = arr copies the reference — both variables point to the same array. Changing one changes the other. const a = [...arr] creates a shallow copy — a new array with the same values. Changes to a don't affect arr.
Q3. Why does new Array(5) create empty slots instead of [5]?
When new Array() receives a single numeric argument, it treats it as the desired length, not as an element. Use Array.of(5) or [5] to create an array containing the number 5.
Q4. What is zero-based indexing?
Arrays start counting from 0. The first element is arr[0], the second is arr[1], and the last is arr[arr.length - 1]. Accessing arr[arr.length] returns undefined.
Q5. What is a sparse array?
A sparse array has "holes" — indices with no assigned value. Example: const a = [1, , , 4]. a[1] is undefined. Sparse arrays are created by delete arr[i] or arr.length = bigNumber. Most array methods skip holes.
Q6. How do you check if a variable is an array?
Use Array.isArray(variable). Don't use typeof because typeof [] returns "object".
Q7. What is the difference between arr.at(-1) and arr[arr.length - 1]?
Both return the last element. arr.at(-1) is cleaner and supports negative indexing natively (ES2022). arr[arr.length - 1] works in all environments.
Q8. Can arrays hold different data types in JavaScript?
Yes. JavaScript arrays are heterogeneous — they can hold numbers, strings, booleans, objects, functions, null, undefined, and other arrays all in the same array.
Key Takeaways
- Arrays are ordered, zero-indexed collections of any data type
- Use array literals
[]— notnew Array()— to avoid the length gotcha - Arrays are reference types — assignment copies the pointer, not the data
- Use
Array.isArray()to check if something is an array - Use
arr.at(-1)for the last element (ES2022+) deleteleaves holes — usesplice()to properly remove elementslengthis always one more than the highest index
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Array Basics - Complete Beginner.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, arrays, array, basics
Related JavaScript Master Course Topics