100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Arrays in JavaScript

Understand how JavaScript arrays store ordered lists of values, how indexing and length work, and why `typeof` alone can't identify an array.

Objects & ArraysBeginner10 min readJul 8, 2026
Analogies

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

javascript
// 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); // true

3. 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

javascript
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

text
apple
3
[ 'apple', 'banana', 'cherry', 'date' ]
object
true
5

6. Key Takeaways

  • Arrays store ordered lists of values, accessed via zero-based numeric indexes.
  • length automatically tracks the highest index and updates as elements are added or removed.
  • Arrays can hold mixed types, including other arrays and objects.
  • typeof on an array returns "object" — use Array.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

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#ArraysInJavaScript#Arrays#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris