Warm-up · Activity 1 of 7
// B2.1 · ~30 min · Beginner
Primitive types and typeof
After this lesson you can name the type of any JavaScript value, predict what + does with text and numbers, and convert values on purpose with Number(), String() and Boolean().
Lesson 1 of 5 in B2 Grammar, types and operators
You will be able to
- Name the seven primitive types and object, and check a value’s type with typeof
- Predict what + does with strings and numbers, and what -, * and / do with strings
- Convert values with Number(), String() and Boolean(), and know which give NaN, 0 or false
Predict · Activity 2 of 7
Predict before you read on: what does this print?
console.log("5" + 2, "5" - 2);Practice · Activity 3 of 7
Fill in a value so that the program prints boolean.
console.log(typeof ____);console.log(typeof );Practice · Activity 4 of 7
Match each expression to what console.log prints for it.
Practice · Activity 5 of 7
A form field holds "12px". What does this print?
console.log(Number("12px"));Brain teaser · Activity 6 of 7
Brain teaser. What does this print?
console.log(1 + 2 + "3", "1" + 2 + 3);Apply · Activity 7 of 7
Mini-task. A shop page gives you the price "19.99" as a string, and shipping costs 4.5. Print the type of both, then the wrong total you get with price + shipping, then the right total using Number(). Finish with Boolean("") to see what an empty form field counts as.
Check your work against this list
Build it yourself
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
Reading an order form
Values typed into a web form arrive as strings, even when they look like numbers. This program shows what that does to a calculation, converts the values on purpose, and checks a few types with typeof. Run it, then change quantity to "three" and see what Number() makes of it.
main.js
// Values from a web form always arrive as text.
const quantity = "3";
const price = "4.50";
const giftNote = "";
console.log(typeof quantity, typeof price);
console.log("joined:", quantity + price);
console.log("total:", Number(quantity) * Number(price));
console.log("gift note?", Boolean(giftNote));
console.log("price as text:", String(Number(price)));
console.log(typeof null, typeof undefined, typeof 10n);
Run it with
node main.jsOutput
string string
joined: 34.50
total: 13.5
gift note? false
price as text: 4.5
object undefined bigint- quantity + price joins the two strings: "3" and "4.50" become "34.50".
- Number() turns each string into a number first, so * multiplies: 3 × 4.5 is 13.5.
- An empty string converts to false, so Boolean(giftNote) tells you the field was left empty.
- Number("4.50") is 4.5; String() turns it back into text without the extra zero.
- typeof null is "object", an old mistake in the language that was kept.
Change it and run it
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Exercises
Exercise 1 of 2
Count the basket
Two quantities come from a form as strings, "2" and "3". The program should print Items: 5 and then Type: number, but it prints Items: 23 and Type: string. Convert the quantities with Number() before adding them, and keep the two console.log lines.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Hints
Hint 1
apples + pears joins two strings: "2" + "3" is "23".
Hint 2
Number("2") is the number 2. Convert each quantity before the +.
Hint 3
const total = Number(apples) + Number(pears);
Show a solution
One way to solve it. Yours can look different and still pass the checks.
// Two quantities typed into a form arrive as text.
const apples = "2";
const pears = "3";
const total = Number(apples) + Number(pears);
console.log("Items:", total);
console.log("Type:", typeof total);
Run it on your computer
Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
// Two quantities typed into a form arrive as text.
const apples = "2";
const pears = "3";
const total = apples + pears;
console.log("Items:", total);
console.log("Type:", typeof total);
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
const lines = async () => (await runMain()).trimEnd().split('\n');
test('prints the number of items', async () => {
const got = await lines();
assert.equal(got[0], 'Items: 5', `the first line is ${JSON.stringify(got[0])}, expected "Items: 5"`);
});
test('the total is a number', async () => {
const got = await lines();
assert.equal(got[1], 'Type: number', `the second line is ${JSON.stringify(got[1])}, expected "Type: number"`);
});
test('prints two lines', async () => {
const got = await lines();
assert.equal(got.length, 2, `the program printed ${got.length} line(s), expected 2`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsExercise 2 of 2
True or false?
Print, on one line, what Boolean() makes of these six values, in this order: 0, "0", "", " " (one space), null and "false". Pass all six results to one console.log. Predict each one before you run the program.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Hints
Hint 1
console.log(Boolean(0), Boolean("0")) prints two results on one line; add the other four the same way.
Hint 2
Only the empty string "" is false among strings; "0", " " and "false" all contain characters.
Hint 3
0 and null both turn into false.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
console.log(Boolean(0), Boolean("0"), Boolean(""), Boolean(" "), Boolean(null), Boolean("false"));
Run it on your computer
Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
// Print Boolean(value) for each value, all on one line:
// 0, "0", "", " ", null, "false"
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
test('prints one line', async () => {
const got = (await runMain()).trimEnd().split('\n');
assert.equal(got.length, 1, `the program printed ${got.length} line(s), expected 1`);
});
test('prints the six results in order', async () => {
const got = (await runMain()).trimEnd();
assert.equal(got, 'false true false true false true', `the program printed ${JSON.stringify(got)}`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsCommon mistakes
Mixing a bigint and a number
const big = 10n;
console.log(big + 1);
What Node.js prints
TypeError: Cannot mix BigInt and other types, use explicit conversionsWhy, and the fix
A bigint and a number are different types, and arithmetic does not convert between them for you, because a large bigint could lose digits as a number. Keep both sides bigints, big + 1n, or convert on purpose: Number(big) + 1. Most programs never need bigint; use it only for whole numbers too large for a number.
Treating null like a string
const nickname = null;
console.log(nickname.length);
What Node.js prints
TypeError: Cannot read properties of null (reading 'length')Why, and the fix
null means “no value”, so it has no length and no other properties. Reading one stops the program; undefined gives the same error with undefined in the message. Check where the value came from: if a name can be missing, give it a string such as "" instead of null, or handle the missing case first (module B3 shows how with if).
Joining a symbol with +
const id = Symbol("id");
console.log("id: " + id);
What Node.js prints
TypeError: Cannot convert a Symbol value to a stringWhy, and the fix
A symbol is a unique id, not text, and + refuses to turn it into a string silently. If you need to show it, ask for its description on purpose: String(id) gives "Symbol(id)". In this course, symbols appear rarely; the point is that + converts most values without asking, but not this one.
JavaScript in the browser: your browser’s own engine, in a sandboxed worker. Syntax errors are located with acorn 8.18.0, MIT. Licence and source
Exit ticket
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.