1. Introduction
An array is an ordered, index-based collection used to store a list of values — numbers, strings, objects, or even other arrays. Arrays in JavaScript are dynamic (they grow and shrink automatically) and can hold values of mixed types in the same array, unlike arrays in many statically typed languages.
Cricket analogy: A batting line-up is an ordered list where each slot has a fixed position, and unlike rigid systems, IPL squads can mix specialists like a batsman, spinner, or wicketkeeper in the same eleven and add impact players mid-season.
2. Syntax
// Array literal syntax
const colors = ["red", "green", "blue"];
// Accessing by zero-based index
colors[0]; // "red"
colors.length; // 3
// Adding an element by index or method
colors[3] = "yellow";
colors.push("purple");
// Checking whether a value is truly an array
Array.isArray(colors); // true3. Explanation
Arrays are created with square-bracket literal syntax [ ] and elements are accessed by a zero-based numeric index, so the first element is arr[0] and the last is arr[arr.length - 1]. The length property always reflects the highest index plus one, and updates automatically when you add or remove elements.
Cricket analogy: The batting order starts at position zero conceptually with the opener first, and the scoreboard's wickets-remaining count automatically updates the moment the tenth wicket falls, just like length recalculates after every change.
Like plain objects, arrays are reference types — copying an array variable copies the reference, so the same aliasing behavior applies (mutating a copied variable mutates the original array too).
Cricket analogy: Handing a teammate your exact scorebook, not a photocopy, means anything they scribble in it changes your original records too, since you both hold the same physical book.
Gotcha: typeof cannot distinguish an array from a plain object — typeof [] returns "object", because arrays are internally implemented as specialized objects. Always use Array.isArray(value) to reliably check whether a value is an array.
4. Example
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[0]);
console.log(fruits.length);
fruits[3] = 'date';
console.log(fruits);
console.log(typeof fruits);
console.log(Array.isArray(fruits));
// Reference type trap
const fruitsCopy = fruits;
fruitsCopy.push('elderberry');
console.log(fruits.length);5. Output
apple
3
[ 'apple', 'banana', 'cherry', 'date' ]
object
true
56. Key Takeaways
- Arrays store ordered lists of values, accessed via zero-based numeric indexes.
lengthautomatically tracks the highest index and updates as elements are added or removed.- Arrays can hold mixed types, including other arrays and objects.
typeofon an array returns "object" — useArray.isArray()to reliably detect arrays.- Arrays are reference types, so assigning one array variable to another aliases the same array (same mutation trap as objects).
Practice what you learned
1. What does `typeof ['a', 'b', 'c']` evaluate to?
2. How do you reliably check whether a variable holds an array rather than a plain object?
3. Given `const arr = [1, 2, 3]; arr[5] = 6;`, what is `arr.length` afterward?
4. What is printed? const a = [1, 2]; const b = a; b.push(3); console.log(a.length);
5. Which index accesses the last element of an array named `list`?
Was this page helpful?
You May Also Like
Objects in JavaScript
Learn how to create, access, and modify JavaScript objects, and understand why objects behave as reference types.
Common Array Methods in JavaScript
Explore the most-used array methods, split into mutating methods that change the original array and non-mutating methods that return a new array.
Spread and Rest Operators in JavaScript
Understand the dual use of the `...` syntax: spreading elements out to copy or merge arrays/objects, and collecting elements into a rest parameter.
Destructuring in JavaScript
Learn how to unpack values from arrays and properties from objects into distinct variables using destructuring, including defaults and renaming.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics