Arrays: create, read and modify
An array is an ordered list of values stored in a single variable. Instead of creating name1, name2 and name3, you create one array with all the names. It is the most used data structure in JavaScript: lists of products, messages, tasks, search results. It is written with brackets and elements separated by commas; they can be of any type, even mixed.
Indexes and length
Each element has a position called an index, and the first one is 0, not 1. The length property tells you how many elements there are, so the last one is at length - 1. You can also use at(-1) for the last, at(-2) for the second to last, and so on. If you ask for an index that does not exist you get undefined, not an error, which sometimes hides silent failures.
const colors = ['red', 'green', 'blue'];
console.log(colors[0]); // red
console.log(colors.length); // 3
console.log(colors[colors.length - 1]); // blue
console.log(colors.at(-1)); // blue
console.log(colors[10]); // undefined
colors[1] = 'yellow'; // const allows changing inside
console.log(colors); // [ 'red', 'yellow', 'blue' ]Adding and removing elements
push adds to the end and pop removes the last one; unshift adds to the beginning and shift removes the first one. All four modify the original array (we say they mutate) and return something useful: push and unshift return the new length, pop and shift return the removed element. To cut or insert in the middle there is splice(start, howManyToRemove, ...newItems), and to copy a part without touching the original, slice(start, end).
const tasks = ['study', 'cook'];
tasks.push('walk'); // ['study', 'cook', 'walk']
tasks.unshift('wake up'); // ['wake up', 'study', 'cook', 'walk']
const last = tasks.pop(); // 'walk'
const first = tasks.shift(); // 'wake up'
console.log(tasks, last, first); // [ 'study', 'cook' ] walk wake up
const letters = ['a', 'b', 'c', 'd', 'e'];
const removed = letters.splice(1, 2, 'X'); // remove 'b','c', insert 'X'
console.log(letters, removed); // [ 'a', 'X', 'd', 'e' ] [ 'b', 'c' ]
console.log(letters.slice(1, 3)); // [ 'X', 'd' ]Searching inside an array
includes tells you whether a value is in the array (true or false) and indexOf returns its position, or -1 if it does not exist. To join the elements into a text use join with the separator you want, and to know whether something is really an array use Array.isArray, because typeof returns 'object'. Remember also that const protects the variable, not the content: you can add and remove elements from an array declared with const.
const days = ['mon', 'tue', 'wed'];
console.log(days.includes('tue')); // true
console.log(days.indexOf('wed')); // 2
console.log(days.indexOf('sun')); // -1
console.log(days.join(' - ')); // mon - tue - wed
console.log(Array.isArray(days), typeof days); // true 'object'
// Arrays are compared by reference
console.log([1, 2] === [1, 2]); // falseArrays inside arrays
The elements of an array can be other arrays, which lets you represent tables or grids. To read a value you use two indexes: rows[1][2] means row 1, column 2. Keep in mind that if you copy an array that contains arrays, the inner rows are shared between the copy and the original. Also, the flat method turns an array of arrays into a single list, and Array.from({ length: 3 }, (_, i) => i) creates a list with as many elements as you need. These are useful tools that will appear again when you handle real data, such as rows of a spreadsheet.
const grid = [
[1, 2, 3],
[4, 5, 6]
];
console.log(grid[1][2]); // 6
console.log(grid.flat()); // [ 1, 2, 3, 4, 5, 6 ]
console.log(Array.from({ length: 3 }, (_, i) => i * 2)); // [ 0, 2, 4 ]✎ マイノート
このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字