Menu

Primitive Types

Lesson 2: Primitive Types

What are the primitive types?

Type Values Example
string Text 'Milk 1L', `Order #${id}`
number Integers and decimals 64, 4.5, -1
boolean true or false inStock = true
bigint Very large integers 9007199254740993n
symbol Unique identifiers Symbol('id')
null Intentionally empty deliveredAt = null
undefined Not yet assigned or missing couponCode = undefined

Use lowercase type names. String, Number, and Boolean (capitalised) refer to wrapper objects and should be avoided.

Example

@@CODE_BLOCK_0@@

How it works

  • number covers integers and decimals. There is no separate int or float type.
  • Under strict mode (the default since TypeScript 6.0), null and undefined are not part of other types. A string can never be null unless you write string | null.
  • This rule, called strictNullChecks, prevents the classic "Cannot read properties of undefined" crash.

@@CODE_BLOCK_1@@

Common mistakes

Mistake Effect Fix
Using String instead of string Confusing errors with wrapper objects Always use lowercase primitives
Storing money as decimals (64.5) Rounding errors (0.1 + 0.2 !== 0.3) Store paise as integers, e.g. 6450
Turning off strict to avoid null errors Runtime crashes return Handle null/undefined explicitly

Key takeaways

  • Use lowercase string, number, boolean, bigint, and symbol.
  • With strict null checks, you must say when a value can be null or undefined.

Try it: Create variables for a product's name, price in paise, stock count, and an optional discountCode that can be undefined.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details