JavaScript / Arrays

Array operations (1): includes, indexOf, sort, reverse

Check whether a value is in an array and where it is, and sort or reverse an array with ready-made operations. Baccalaureate

Is it there? includes

Arrays come with ready-made operations that save us writing loops: the array name, a dot, then the operation. For example, list.includes(value) returns true if the value is in the array, and false if it is not:

JavaScript
const wordList = ["dog", "dog", "dog", "dag", "dog"];
console.log(wordList.includes("dag"));
console.log(wordList.includes("cat"));
Console
true
false

Where is it? indexOf

list.indexOf(value) returns the index of the value (from 0); if it appears more than once you get the first position. If it is not there you get -1:

JavaScript
const wordList = ["bat", "bat", "bat", "bat", "bat", "bat", "bit", "bat"];
console.log(wordList.indexOf("bit"));
console.log(wordList.indexOf("pen"));
Console
6
-1
The answer 6 means "bit" is the seventh word. Write indexOf with a capital O, or you get an error.

Sorting with sort, flipping with reverse

list.sort() puts strings in alphabetical order, A to Z, and list.reverse() flips the order so the last becomes first. Both change the array itself, so print it on a line after:

JavaScript
const nameList = ["Emma", "Sophia", "Oliver", "Alex"];
nameList.sort();
console.log(nameList);
nameList.reverse();
console.log(nameList);
Console
["Alex", "Emma", "Oliver", "Sophia"]
["Sophia", "Oliver", "Emma", "Alex"]
sort() with nothing in the brackets treats every value as text, even numbers! It compares the first digit first, so 100 comes before 25 and 9:
JavaScript
const nums = [25, 100, 9, 41];
nums.sort();
console.log(nums);
Console
[100, 25, 41, 9]

To sort numbers, give sort a compare function that takes two values a and b and returns a - b: a negative result puts a first. For largest to smallest, return b - a:

JavaScript
function smallFirst(a, b) {
  return a - b;
}
function bigFirst(a, b) {
  return b - a;
}

const nums = [25, 100, 9, 41];
nums.sort(smallFirst);
console.log(nums);
nums.sort(bigFirst);
console.log(nums);
Console
[9, 25, 41, 100]
[100, 41, 25, 9]

Practice

1Fill in the blankCurriculum practice

Print whether the word list contains "bell". (Output: true)

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const wordList = ["ball", "ball", "ball", "ball", "bell", "ball", "ball"];

console.log(wordList.includes(___));
Console
Press Run to see the result.
2Write the programCurriculum practice

On line 3, print the index of the word "pen" in the word list.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const wordList = ["pan", "pan", "pen", "pan", "pan", "pan", "pan", "pan"];

Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Complete line 3 to sort guestList alphabetically before it is printed.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let guestList = ["Johnson", "Smith", "Brown", "Davis", "Wilson"];

___
console.log(guestList);
Console
Press Run to see the result.
4Write the programCurriculum practice

From line 3: reverse the order of rankingList, then print it.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const rankingList = ["Mason", "Isabella", "William", "Charlotte", "Mohamed"];

Console
Press Run to see the result.
5Write the programExtra practice

Sort the marks from smallest to largest as numbers and print them. Output: [9, 23, 45, 78, 100]

Hintmarks.sort() alone gives [100, 23, 45, 78, 9]. You need a compare function that returns a - b.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const marks = [45, 100, 9, 78, 23];
Console
Press Run to see the result.
6Write the programExtra practice

Competition scores. Sort them from largest to smallest and print the array, then print the winning score (the first value). Output: [100, 95, 91, 88, 72] then Winner: 100.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const scores = [88, 95, 72, 100, 91];
Console
Press Run to see the result.
7Write the programExtra practice

The canteen queue. Print Omar's place in the queue, counting from 1: Omar is number 3. Use indexOf.

HintindexOf starts at 0, so add 1. Put the addition in brackets so it is not glued on as text.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const queue = ["Ali", "Mona", "Omar", "Sara"];
Console
Press Run to see the result.

Key points

  • list.includes(v) → true or false; list.indexOf(v) → the index, or -1.
  • list.sort() sorts alphabetically, list.reverse() flips the order; both change the array itself.
  • To sort numbers: list.sort(f) where f(a, b) returns a - b (or b - a for largest first).