Course progress0%
Course content

Module 1

Module 2

Module 3

Module 4

Module 5

Module 6

Module 7

Module 8

Module 9

Module 10

Module 11

Module 12

Module 13

Module 14

Module 15

Module 16

Module 17

Module 18

30 min

Primitive Data Types

Choose data types according to meaning

By the end of this lesson

  • Choose data types according to meaning
  • Distinguish missing, empty and explicitly absent values

Start with meaning

A delivery address contains the building number "007". Should it be stored as the number 7? Pause and explain what information that conversion would lose.

Identifiers are often text even when they contain digits. Arithmetic quantities are usually numbers. Choose a representation because of its meaning, not its appearance.

The values we need first

Type or value Meaning in an example
number A quantity or measured amount, such as 3
string Text or an identifier, such as "007"
boolean A yes/no result: true or false
undefined A value has not been supplied or assigned
null An explicit absence, when that is the agreed convention

JavaScript also has BigInt and Symbol; they are outside this introductory course. A variable can hold values of different types over time, but surprising type changes make reasoning harder.

const buildingCode = "007";
const quantity = 3;
const collected = false;
let collectionTime;
const assignedCourier = null;
console.assert(typeof buildingCode === "string");
console.assert(typeof quantity === "number");
console.assert(typeof collected === "boolean");
console.assert(collectionTime === undefined);
console.assert(assignedCourier === null);

typeof produces a string describing a value's type. The historical result typeof null is "object"; test value === null when you need to identify null. It does not mean null is a useful object you can read properties from.

Guided review: absence is not zero

An unknown delivery charge and a confirmed free delivery are different. Use an explicit missing-data convention for the unknown charge; use 0 when free delivery is confirmed. Otherwise the program can produce a plausible but unsupported quote.

Independent exercise

Choose values for an order reference "0008", item count 0, collection status false and an explicitly unassigned courier. Explain why an empty string is not automatically interchangeable with null.

Correction

The reference is a string, count a number, status a Boolean and courier can be null under the chosen convention. "" is existing text with zero characters; null expresses absence. Define what each means before accepting input. Empty text might be invalid rather than a separate valid state.

Check your reasoning

Can typeof alone decide whether a number is a valid quantity? No. Type describes representation; validity also depends on the rule, such as a positive whole number. We will enforce that distinction later.

Lesson complete?

Your progress is saved on this device.