liveBook · Manning

Undefined

As explained in Essential TypeScript 5, Third Edition

undefined is the JavaScript primitive value that indicates that a value has not been assigned or is unavailable.

`undefined` is the JavaScript value of a variable that has been declared but not assigned. A property read also returns `undefined` when the property does not exist. Because an object's properties can change, code should not assume that every expected value is present. section 7.2 section 3.5.1

An unassigned value

In `src/index.ts`, `let myVar; console.log(`${myVar} = ${typeof myVar}`);` prints `undefined = undefined`. Assigning `12` afterward makes `typeof myVar` report `number`. section 7.2

Use `??` when a fallback should apply only to `null` or `undefined`. The logical OR operator, `||`, also replaces valid falsy values such as `0` and `""`. For example, `taxRate || 10` gives `10` when `taxRate` is zero; `taxRate ?? 10` preserves zero. section 3.3.2

Listing 3.13 Using the nullish operator in the index.js file in the primer folder
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);

let taxRate; // no tax rate has been defined
console.log(`Tax rate: ${taxRate ?? 10}%`);
taxRate = 0; // zero-rated for tax
console.log(`Tax rate: ${taxRate ?? 10}%`);

The `index.js` listing tests an unassigned tax rate and then a zero rate with `??`, preserving the distinction between missing and zero. section 3.3.2

For an object that might itself be missing, `hat?.price ?? 0` avoids reading `price` from a `null` or `undefined` `hat` and supplies a fallback if the resulting price is missing. section 3.5.1

Listing 3.29 Using optional chaining in the index.js file in the primer folder
let hat = {
    name: "Hat",
    price: 100
};

let boots = {
    name: "Boots",
    price: "100"
}

let gloves = {
    productName: "Gloves",
    price: "40"
}

gloves.name = gloves.productName;
delete gloves.productName;
gloves.price = 20;

let propertyCheck = hat.price ?? 0;
let objectAndPropertyCheck = hat?.price ?? 0;
console.log(`Checks: ${propertyCheck}, ${objectAndPropertyCheck}`);

The `index.js` listing places optional chaining before the property read and nullish coalescing after it. section 3.5.1

TypeScript's `strictNullChecks` option prevents `null` and `undefined` from being accepted as values of other types; without it, those values can conceal type inconsistencies. If a type includes a missing value, check it before using properties or methods. A non-null assertion (`!`) tells the compiler to disregard that possibility, but can leave a runtime error if the value is actually missing. section 7.0 section 7.7.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through variable output, object-property access, and fallback operators; addresses TypeScript checking without exploring runtime implementation.
Examples
Tests an unassigned variable, a zero tax rate, and a potentially missing object property in `index.ts` and `index.js`.
What it assumes
Starts with JavaScript assignments and `typeof`; teaches the unassigned-value case through output before addressing TypeScript checks.
What you can do after
Operate/configure it with `strictNullChecks`, and guard missing values with `??` or optional chaining.
How it connects
Connects undefined to type coercion, nullish coalescing, optional chaining, strictNullChecks, type guards, and non-null assertions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.