JavaScript / Arrays

Data types and arrays

Find the type of any value with typeof, store many values in one array, and read or change them by their position.

Every value in JavaScript has a type. Numbers such as 100 and 36.5 are number. Text inside quotes such as "Hello" is a string. The yes/no values true and false are boolean. The word typeof tells you the type of a value:

JavaScript
console.log(typeof 500);
console.log(typeof "500 coins");
console.log(typeof "100");
console.log(typeof true);
console.log(typeof (3 > 5));
Console
number
string
string
boolean
boolean

The type changes the result: + adds two numbers, but with a string it glues them together. That is why we convert with Number() and String(). Careful: "100" in quotes is a string, not a number, and Number("Hello") gives NaN, "Not a Number":

JavaScript
console.log("500" + 5);
console.log(Number("500") + 5);
console.log(String(500) + 5);
console.log(Number("Hello"));
Console
5005
505
5005
NaN

Arrays

To store a week of temperatures you would need seven variables. It is easier to put them all in one variable called an array: values inside square brackets [ ], separated by commas. Each value has a position number (index) that starts at 0; you write it in square brackets after the array name.

JavaScript
let tempList = [36, 36, 26, 27, 28, 33, 36];
console.log(tempList);
console.log(tempList[0]);
console.log(tempList[4]);
Console
[36, 36, 26, 27, 28, 33, 36]
36
28
Index0123456
Value36362627283336
Counting starts at 0: the fifth value is tempList[4], not tempList[5].

tempList.length gives the number of values in the array. Since counting starts at 0, the last value is at length - 1. Asking for a position that does not exist gives undefined:

JavaScript
let tempList = [36, 36, 26, 27, 28, 33, 36];
console.log(tempList.length);
console.log(tempList[tempList.length - 1]);
console.log(tempList[7]);
Console
7
36
undefined

Changing a value

Change a value inside an array by assigning to its position, just like any variable. Arrays can hold strings too; when printed, the strings appear in quotes:

JavaScript
let marks = [70, 85, 90];
marks[1] = 95;
console.log(marks);

let names = ["Mona", "Omar", "Sara"];
names[0] = "Laila";
console.log(names);
Console
[70, 95, 90]
["Laila", "Omar", "Sara"]

Practice

1Write the programCurriculum practice

Print the data type of the value "100" using typeof.

HintKeep the quotes; the output should be string.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Fill the blank to put the values 5, 10 and 15 into the array num, in that order.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let num = [___];
console.log(num);
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Print the value 33 from the array without typing 33 directly.

HintCount from 0.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let tempList = [36, 36, 26, 27, 28, 33, 36];
console.log(___);
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Print the number of values in the array without typing the number directly.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let num = [2, 4, 6, 8, 10];
console.log(___);
Console
Press Run to see the result.
5Write the programCurriculum practice

The temperatures are 10, 15, 13, 16, 14, 12 and 17. Store them in an array called tempList, then print the third value (13).

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Edit the programExtra practice

The science teacher is absent, so the third period becomes "PE". Add a line that changes "Science" to "PE" using its index, before printing. Output: ["Math", "Arabic", "PE", "English"]

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let periods = ["Math", "Arabic", "Science", "English"];

console.log(periods);
Console
Press Run to see the result.
7Write the programExtra practice

Print the first name in the queue, then the last one. For the last name use length, do not type 4. Output: Ali then Youssef.

HintThe last index is queue.length - 1.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let queue = ["Ali", "Mona", "Omar", "Sara", "Youssef"];
Console
Press Run to see the result.

Key points

  • typeof shows the type: number, string or boolean; Number() and String() convert.
  • An array is [a, b, c]; the first value is list[0], the last is list[list.length - 1].
  • list.length is the number of values; list[i] = x changes one.