Values, Types, and Expressions
Primitive Types
Work with numbers and booleans, and read a value's type.
primitive-types
Numbers and booleans are primitive values. A comparison yields a boolean, and `typeof` reports the type name as a string.
Primitive Types
primitive_types.js
Replay: real traced execution (multi-file project)
const n = 42;
const big = n > 10;
const kind = typeof n;
console.log("n=" + n);
console.log("big=" + big);
console.log("kind=" + kind);
const n = 7;
const big = n > 10;
const kind = typeof n;
console.log("n=" + n);
console.log("big=" + big);
console.log("kind=" + kind);
const n = 100;
const big = n > 10;
const kind = typeof n;
console.log("n=" + n);
console.log("big=" + big);
console.log("kind=" + kind);
n ← 42, big ← true, kind ← number
1const n→ 42 = 42; //@n=7, 1002const big→ true = n→ 42 > 10;3const kind→ number = typeof n;45console.log("n=" + n42);6console.log("big=" + bigtrue);7console.log("kind=" + kindnumber);outputn=42 big=true kind=number
n ← 7, big ← false, kind ← number
1const n→ 7 = 7;2const big→ false = n→ 7 > 10;3const kind→ number = typeof n;45console.log("n=" + n7);6console.log("big=" + bigfalse);7console.log("kind=" + kindnumber);outputn=7 big=false kind=number
n ← 100, big ← true, kind ← number
1const n→ 100 = 100;2const big→ true = n→ 100 > 10;3const kind→ number = typeof n;45console.log("n=" + n100);6console.log("big=" + bigtrue);7console.log("kind=" + kindnumber);outputn=100 big=true kind=number
Follow the Primitives
nstarts at42.big = n > 10becomestrue.kind = typeof nbecomesnumber.- The program prints
n=42,big=true, andkind=number. | n | big | kind | | --- | --- | --- | | 7 | false | number | | 42 | true | number | | 100 | true | number |
Exercise: primitive_types.js
Reproduce big=true and kind=number for n=42, then use the pinned n values 7 and 100 to predict big.