Numbers and Strings
Numbers and Strings
π¨βπΌ Perfect! You've created your first typed variables.
Notice how TypeScript gives you autocomplete and error checking:
- If you tried to assign a string to
price, TypeScript would catch it - Template literals automatically convert values to strings when interpolating
π¦ In practice, TypeScript can often infer these types:
const price = 29.99 // TypeScript infers: number
const productName = 'TypeScript Guide' // TypeScript infers: string
The explicit type annotations (
: number, : string) are optional here but
help document your intent. We'll see when they become essential in later
exercises.You might notice JavaScript sometimes produces surprising decimal results like
0.1 + 0.2 = 0.30000000000000004. This is due to how computers store decimal
numbers in binary (floating point representation). It happens in most
programming languages, not just JavaScript. For currency calculations in real
apps, consider working in cents (integers) or using specialized libraries.

