All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

1D and 2D arrays in JavaScript

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Arrays store lists of values under one name. The specification requires 1D and 2D data structures (spec 9.3.2 c); exam questions ask you to declare, populate, search and total arrays.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

1D arrays

const towns = ["Leeds", "York", "Hull"];
console.log(towns[0]);         // first item: index 0
console.log(towns.length);
towns[1] = "Ripon";            // change an item
towns.push("Selby");           // add to the end
let last = towns.pop();        // remove and return the last item
towns.unshift("Bath");         // add to the start
console.log(towns.join(", "));
console.log(towns.indexOf("Hull"), towns.includes("York"));

Output:

Leeds
3
Bath, Leeds, Ripon, Hull
3 false

const stops the variable being given a new array, but the items can still change. indexOf returns -1 if the item is not found.

Useful array methods

const scores = [42, 7, 19, 88, 23];
console.log(Math.max(...scores), Math.min(...scores));
const sorted = [...scores].sort((a, b) => a - b);   // numeric sort, copy first
console.log(sorted.join(" "));
const words = ["pear", "Apple", "fig"];
words.sort();
console.log(words.join(" "));
const high = scores.filter(s => s > 20);
console.log(high.join(" "));
console.log(scores.slice(1, 3).join(" "));

Output:

88 7
7 19 23 42 88
Apple fig pear
42 88 23
7 19

sort() with no function sorts as text: capital letters before lower case, and numbers by their digits (so 100 comes before 25). For numbers use sort((a, b) => a - b). filter keeps items that pass a test; slice(start, end) copies part of an array (end not included); splice(index, count) removes items.

2D arrays

A 2D array is an array of arrays: a table of rows and columns. array[row][column] reads one value.

const results = [
  ["Ali", 72, 65],
  ["Bea", 88, 91],
  ["Cai", 54, 60]
];
console.log(results[1][0] + " scored " + results[1][2]);
for (let r = 0; r < results.length; r++) {
  let total = 0;
  for (let c = 1; c < results[r].length; c++) {
    total += results[r][c];
  }
  console.log(results[r][0] + ": " + total);
}

Output:

Bea scored 91
Ali: 137
Bea: 179
Cai: 114

Nested loops visit every cell: the outer loop goes through the rows and the inner loop through the columns of that row.

Worked example

Find the highest and the average of the marks [56, 71, 48, 90, 65] using a loop (not Math.max).

  1. Start highest at the first item and total at 0.
  2. Loop through every mark: add it to total; if it is greater than highest, store it.
  3. Average = total / number of marks.

Answer: ```js const marks = [56, 71, 48, 90, 65]; let highest = marks[0]; let total = 0; for (let i = 0; i < marks.length; i++) { total += marks[i]; if (marks[i] > highest) { highest = marks[i]; } } console.log("Highest: " + highest); console.log("Average: " + total / marks.length); ``` Output: ``` Highest: 90 Average: 66 ```

Key idea: Arrays are zero-indexed: the first item is [0] and the last is [length - 1]. push and pop work at the end, unshift at the start; indexOf, includes, join, sort, filter, slice. A 2D array is read as array[row][column] with nested loops.

Check yourself

  1. What is displayed when this code runs? ```js const days = ["Mon", "Tue", "Wed", "Thu"]; console.log(days[2]); ```
    Show answer

    Wed. Indexes start at 0, so days[2] is the third item: Wed.

  2. What is displayed when this code runs? ```js const stack = [5, 9]; stack.push(4); stack.push(7); stack.pop(); console.log(stack.length + " " + stack[stack.length - 1]); ```
    Show answer

    3 4. After push 4 and 7 the array is 5, 9, 4, 7; pop removes 7, leaving 3 items with 4 last: 3 4.

  3. What is displayed when this code runs? ```js const grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log(grid[2][1] + grid[0][2]); ```
    Show answer

    11. grid[2][1] is row 2, column 1: 8. grid[0][2] is 3. 8 + 3 = 11.

  4. What is displayed when this code runs? ```js const n = [100, 25, 3]; n.sort(); console.log(n.join(" ")); ```
    • 3 25 100
    • 100 3 25
    • 100 25 3
    • 25 3 100
    Show answer

    100 25 3. sort() with no comparison function sorts as text, comparing the first characters 1, 2 and 3: 100 25 3. Use sort((a, b) => a - b) for numbers.

  5. What is displayed when this code runs? ```js const colours = ["red", "green", "blue"]; console.log(colours.indexOf("purple")); ```
    Show answer

    -1. indexOf returns -1 when the item is not in the array.

  6. The array should hold the three sizes Small, Medium and Large, so that the loop displays them in order. Which line completes the code? ```js __________ for (const s of sizes) { console.log(s); } ```
    • `const sizes = "Small", "Medium", "Large";`
    • `const sizes = {Small, Medium, Large};`
    • `const sizes = ["Small", "Medium", "Large"];`
    • `const sizes = ("Small", "Medium", "Large");`
    Show answer

    `const sizes = ["Small", "Medium", "Large"];`. An array literal uses square brackets, with quoted strings separated by commas.

  7. What is displayed when this code runs? ```js const temps = [[18, 22, 25], [15, 30, 28]]; let hot = 0; for (const row of temps) { for (const t of row) { if (t >= 25) { hot++; } } } console.log(hot); ```
    Show answer

    3. Values of at least 25: 25, 30 and 28, so 3.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.