Arrays
An array is an ordered list. Shopping lists, scores, users, messages — whenever you have “many of something”, you use an array.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 27, 2026
Related tutorials
Learning objectives#
- Create arrays and read items by index
- Add and remove items at both ends
- Search arrays with includes and indexOf
- Copy parts of arrays with slice
Explanation#
Arrays use square brackets: ['red', 'green']. Items are numbered from 0, so the first item is colors[0].
length tells you how many items there are. at(-1) gets the last item.
push adds to the end, pop removes from the end; unshift adds to the start, shift removes from the start.
includes(x) answers “is it there?”, indexOf(x) answers “where?” (or -1). slice(start, end) copies a part without changing the original.
Code example: A colour list#
const colors = ['red', 'green'];
colors.push('blue');
colors.unshift('black');
console.log(colors, colors.length);
console.log(colors[0], colors.at(-1));
console.log(colors.includes('green'), colors.indexOf('blue'));
const last = colors.pop();
console.log(last, colors);
console.log(colors.slice(1, 3));
[ 'black', 'red', 'green', 'blue' ] 4 black blue true 3 blue [ 'black', 'red', 'green' ] [ 'red', 'green' ]
Step-by-step explanation#
- push adds "blue" at the end; unshift adds "black" at the start.
- Index 0 is the first item; at(-1) is the last.
- "blue" is at index 3 (counting from 0).
- pop removes and returns the last item.
- slice(1, 3) copies indexes 1 and 2 — the end index is not included.
Real-world use case#
A to-do app stores tasks in an array: push to add, filter to remove, map to display.
Common mistakes#
The last index is length - 1, not length. colors[colors.length] is undefined.
slice copies and leaves the original alone; splice removes or inserts items and changes the original.
Best practices#
- Use const for arrays (you can still change their contents)
- Use at(-1) for the last item
- Use includes for yes/no checks
- Prefer methods that return new arrays when working with React state
Remember this#
Arrays start counting at 0. Last index = length − 1.
push/pop work at the END (like a stack of plates). shift/unshift work at the START.
sLice = Look (copy). sPlice = sPlit and change.
Practice exercise#
Your task: Start with [10, 20, 30]. Add 40 at the end and 5 at the start, then print the array, its length and whether it includes 25.
Show solution
const nums = [10, 20, 30];
nums.push(40);
nums.unshift(5);
console.log(nums, nums.length, nums.includes(25)); // [ 5, 10, 20, 30, 40 ] 5 false
Mini quiz#
What is printed?
const a = ['x', 'y', 'z'];
console.log(a[1], a.length);
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.