Create arrays and access indices
Quick answer
An array is an object that represents an ordered collection of values using zero-based integer indices. An array literal uses square brackets.
How it works
Use [first, second] and items[0]. Reading an absent index ordinarily returns undefined. Array.isArray(value) identifies genuine arrays, unlike typeof which returns object. Arrays can mix types, though a consistent element type often simplifies processing.
const colors = ["red", "blue"];console.log(colors[0], colors[2]);Expected output in Node.js:
red undefinedIndex zero selects the first value; index two is absent.
const scores = [4, 9, 6];console.log(Array.isArray(scores), scores[1]);Expected output in Node.js:
true 9The literal is a genuine array and index one selects the second item.
Common mistakes
Do not start counting indices at one. Avoid Array(3) when you intend [3]; the constructor with a single integer creates empty positions.
Practice
Create an array of three names and print the first and last.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Create arrays and access indices — Knowledge Check
Apply the taught concept and reason about the result.
What index identifies the first element of an array?
What does [7,9][1] evaluate to?
What does Array.isArray({length:2}) return?
What is the difference between Array(3) and [3]?
What is typeof []?