Data Types
Primitive Types
Start with TypeScript's everyday primitive values: numbers, booleans, and strings.
primitive type
Primitive values are single values such as `number`, `boolean`, and `string`.
Primitive Types
primitives.ts
Replay: real traced execution (multi-file project)
const price: number = 18;
const inStock: boolean = true;
const itemName: string = "marker";
const taxRate: number = 0.08;
const tax: number = price * taxRate;
const total: number = price + tax;
console.log(`${itemName} available: ${inStock}`);
console.log(`price=${price}`);
console.log(`total=${total}`);
const price: number = 12;
const inStock: boolean = true;
const itemName: string = "marker";
const taxRate: number = 0.08;
const tax: number = price * taxRate;
const total: number = price + tax;
console.log(`${itemName} available: ${inStock}`);
console.log(`price=${price}`);
console.log(`total=${total}`);
const price: number = 24;
const inStock: boolean = true;
const itemName: string = "marker";
const taxRate: number = 0.08;
const tax: number = price * taxRate;
const total: number = price + tax;
console.log(`${itemName} available: ${inStock}`);
console.log(`price=${price}`);
console.log(`total=${total}`);
price ← 18, inStock ← true, itemName ← marker, taxRate ← 0.08
1const price→ 18: number = 18; //@price=12, 242const inStock→ true: boolean = true;3const itemName→ marker: string = "marker";45const taxRate→ 0.08: number = 0.08;6const tax→ 1.44: number = price→ 18 * taxRate0.08;7const total→ 19.44: number = price→ 18 + tax1.44;89console.log(`${itemNamemarker} available: ${inStocktrue}`);10console.log(`price=${price18}`);11console.log(`total=${total19.44}`);outputmarker available: true price=18 total=19.44
price ← 12, inStock ← true, itemName ← marker, taxRate ← 0.08
1const price→ 12: number = 12;2const inStock→ true: boolean = true;3const itemName→ marker: string = "marker";45const taxRate→ 0.08: number = 0.08;6const tax→ 0.96: number = price→ 12 * taxRate0.08;7const total→ 12.96: number = price→ 12 + tax0.96;89console.log(`${itemNamemarker} available: ${inStocktrue}`);10console.log(`price=${price12}`);11console.log(`total=${total12.96}`);outputmarker available: true price=12 total=12.96
price ← 24, inStock ← true, itemName ← marker, taxRate ← 0.08
1const price→ 24: number = 24;2const inStock→ true: boolean = true;3const itemName→ marker: string = "marker";45const taxRate→ 0.08: number = 0.08;6const tax→ 1.92: number = price→ 24 * taxRate0.08;7const total→ 25.92: number = price→ 24 + tax1.92;89console.log(`${itemNamemarker} available: ${inStocktrue}`);10console.log(`price=${price24}`);11console.log(`total=${total25.92}`);outputmarker available: true price=24 total=25.92
Follow the Values
itemNameis"marker".inStockistrue.pricestarts at18.taxis18 * 0.08, which is1.44.totalis19.44, so the program printsmarker available: true,price=18, andtotal=19.44. | price | taxRate | total | | --- | --- | --- | | 12 | 0.08 | 12.96 | | 18 | 0.08 | 19.44 | | 24 | 0.08 | 25.92 |
Exercise: primitives.ts
Reproduce total=19.44 for marker, then use the pinned prices 12 and 24 to predict each total.