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:
console.log(typeof 500); console.log(typeof "500 coins"); console.log(typeof "100"); console.log(typeof true); console.log(typeof (3 > 5));
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":
console.log("500" + 5);
console.log(Number("500") + 5);
console.log(String(500) + 5);
console.log(Number("Hello"));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.
let tempList = [36, 36, 26, 27, 28, 33, 36]; console.log(tempList); console.log(tempList[0]); console.log(tempList[4]);
[36, 36, 26, 27, 28, 33, 36] 36 28
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Value | 36 | 36 | 26 | 27 | 28 | 33 | 36 |
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:
let tempList = [36, 36, 26, 27, 28, 33, 36]; console.log(tempList.length); console.log(tempList[tempList.length - 1]); console.log(tempList[7]);
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:
let marks = [70, 85, 90]; marks[1] = 95; console.log(marks); let names = ["Mona", "Omar", "Sara"]; names[0] = "Laila"; console.log(names);
[70, 95, 90] ["Laila", "Omar", "Sara"]
Practice
Print the data type of the value "100" using typeof.
Hint
Keep the quotes; the output should be string.Press Run to see the result. Fill the blank to put the values 5, 10 and 15 into the array num, in that order.
let num = [___]; console.log(num);
Press Run to see the result. Print the value 33 from the array without typing 33 directly.
Hint
Count from 0.let tempList = [36, 36, 26, 27, 28, 33, 36]; console.log(___);
Press Run to see the result. Print the number of values in the array without typing the number directly.
let num = [2, 4, 6, 8, 10]; console.log(___);
Press Run to see the result. The temperatures are 10, 15, 13, 16, 14, 12 and 17. Store them in an array called tempList, then print the third value (13).
Press Run to see the result. 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"]
let periods = ["Math", "Arabic", "Science", "English"]; console.log(periods);
Press Run to see the result. 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.
Hint
The last index isqueue.length - 1.let queue = ["Ali", "Mona", "Omar", "Sara", "Youssef"];
Press Run to see the result. Key points
typeofshows the type: number, string or boolean;Number()andString()convert.- An array is
[a, b, c]; the first value islist[0], the last islist[list.length - 1]. list.lengthis the number of values;list[i] = xchanges one.