01What is a JavaScript Array?
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
An Array is a special type of object used to store multiple values in a single variable. Unlike a standard object which uses named keys, an array uses numeric indices. This makes arrays perfect for lists where order matters: a list of users, a series of coordinates, or the messages in a chat history.
02Zero-Based Indexing
Every element in an array sits at a numbered position called an index. Critically, the first element is at index 0, not 1. This is called zero-based indexing, and it applies consistently across JavaScript and most programming languages.
03The .length Property
Every array has a built-in .length property that automatically reflects the current number of elements. Add an item with .push() and .length increases by 1. Remove one with .pop() and it decreases. There is no need to track the count manually.
04Reference Types and Memory
This is one of the most important concepts in JavaScript: Arrays are Reference Types. When you write const b = a, you are not duplicating the array — you are copying the *address* in memory where the array lives. Both a and b now point to the same data.
05Arrays Are Objects (typeof Quirk)
Running typeof [] in JavaScript returns 'object', not 'array'. This surprises many developers. The reason: arrays are specialised objects with numeric string keys ('0', '1', '2'…) and an auto-updating length property.
?Frequently Asked Questions
Why do JavaScript arrays start at index 0?
The index represents a memory offset from the start of the allocated block. An offset of 0 points to the very first byte — the start of the array. This convention comes from C and is used by most programming languages.
Can a JavaScript array hold different data types at once?
Yes. JavaScript arrays are untyped. A single array can simultaneously hold strings, numbers, booleans, null, objects, and even other arrays. There is no restriction on mixing types.
How do I make a real copy of an array instead of a reference?
Use the spread operator: `const copy = [...originalArray]`. This creates a shallow copy — a new array at a new memory address. For deeply nested arrays or objects, use `structuredClone(originalArray)` for a true deep copy.
What does typeof [] return in JavaScript?
`typeof []` returns `'object'`, not `'array'`. This is a known quirk. To reliably check if a value is an array, always use `Array.isArray(value)`, which returns `true` for arrays and `false` for everything else.
How do I access the last element of an array?
Use `arr[arr.length - 1]` for broad compatibility, or the modern `arr.at(-1)` (ES2022+). Both retrieve the last element without needing to know the array's size in advance.
What is a nested array in JavaScript?
A nested array is an array that contains other arrays as elements. This is how 2D structures (grids, matrices, tables) are represented. Access with double bracket notation: `grid[row][column]`.
Why do JavaScript arrays start at index 0?
The index represents a memory offset from the start of the allocated block. An offset of 0 means 'start exactly here' — the very first position. This convention originates from C and is standard in most programming languages.
Can a JavaScript array hold different data types at once?
Yes. JavaScript arrays are untyped. A single array can simultaneously hold strings, numbers, booleans, null, objects, and even other arrays. There is no restriction on mixing types.
How do I make a real copy of an array instead of a reference?
Use the spread operator: `const copy = [...originalArray]`. This creates a shallow copy at a new memory address. For deeply nested structures, use `structuredClone(originalArray)` for a complete deep copy.
What does typeof [] return in JavaScript?
`typeof []` returns `'object'`, not `'array'`. To reliably check if a value is an array, always use `Array.isArray(value)`, which returns `true` for arrays and `false` for everything else.
How do I access the last element of an array?
Use `arr[arr.length - 1]` for broad compatibility, or the modern `arr.at(-1)` (ES2022+). Both retrieve the last element dynamically without needing to know the array's current length in advance.
