Chapter 3 · Data

Arrays

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

An array keeps values in order

By the end of this lesson you keep, change and share lists of values.

const scores = [90, 75, 60];
  • An array keeps values in order, between [ and ].
  • Index it as you index text (lesson 9): scores[0] is 90, scores.at(-1) is 60, scores.length is 3.
  • scores[5] is past the end: undefined, not an error.
  • The array lives on the heap. The box holds an arrow to it.
const names = ["Ada", "Alan"];
names.push("Grace");
console.log(names);
names[0] = "Bo";
const last = names.pop();
console.log(names, last);
console.log(names.length);
const scores = [90, 75, 60];
let total = 0;
let best = scores[0];
for (const s of scores) {
  total += s;
  if (s > best) {
    best = s;
  }
}
console.log("Total:", total);
console.log("Best:", best);
console.log("Average:", total / scores.length);
console.log(scores.includes(75), scores.indexOf(60));
for (const n of [3, -1, 4, 0, 5]) {
  if (n < 0) {
    continue;
  }
  if (n === 0) {
    break;
  }
  console.log(n);
}
console.log("Done");
const scores = [72, 45, 90, 50, 38, 66];
const high = [];
// push each score over 50 into high
console.log(high.join(", "));
console.log(`${high.length} scores`);
const a = [1, 2];
const b = a;
const c = [...a];
b.push(3);
console.log(a);
console.log(c);
console.log(a === b, a === c);
function doubled(xs) {
  // a new array: each item of xs times 2
  return xs;
}

const nums = [1, 2, 3];
const twice = doubled(nums);
console.log(nums.join(", "));
console.log(twice.join(", "));
const pair = ["Ada", 36];
const [name, age] = pair;
console.log(name, age);
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b);
const table = [];
for (let r = 1; r <= 3; r++) {
  const row = [];
  for (let c = 1; c <= 3; c++) {
    row.push(r * c);
  }
  table.push(row);
}
for (const row of table) {
  console.log(row.join(" "));
}
const trap = Array(3).fill([]);
trap[0].push("x");
console.log(trap);
// 1. board: 3 rows of 3 ".", one push a round
// 2. "X" at row 0, col 0; "O" at row 1, col 1
// 3. log each row, joined by " "
const board = [
  ["X", "O", "."],
  [".", "X", "O"],
  ["O", ".", "."],
];
for (const row of board) {
  // log the row, " | ", then the row in a mirror
}
console.log("First row:", board[0].join(" "));
Terminal
Step 1 of 10Changes in a row

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)