Skip to main content

JavaScript and TypeScript Primitives

TypeScript improves JavaScript by adding static types — types that are checked before the code runs.

The most basic types in TypeScript are called primitives.

These types are the building blocks for the more complex types in your applications.

TypeScript includes all the JavaScript primitives, plus extra type features of its own.

Here are the five primitive types you will use most often.


Boolean

A boolean holds a true/false value.

Use it for flags, toggles and conditions.

Example

isActive is given the boolean type explicitly. hasPermission has no annotation, so TypeScript infers boolean from the value false.

Prompt
Show the boolean primitive type in two ways. Declare isActive with an explicit boolean annotation set to true, and hasPermission set to false with no annotation, noting in a comment that TypeScript infers boolean. Nothing is printed.
Output: TypeScript file and captured output

Number

A number holds both whole numbers and decimal numbers.

TypeScript uses the same number type for every numeric value, no matter how it is written.

Example

Decimal, hexadecimal, binary, octal and floating-point values all have the type number.

Prompt
Show that TypeScript uses one number type for every numeric literal form. Declare five number variables: decimal (6), hex (0xf00d), binary (0b1010), octal (0o744) and float (3.14), each with a comment naming its notation. Nothing is printed.
Output: TypeScript file and captured output

String

A string holds text.

You can write strings with single quotes ('), double quotes (") or backticks (`). Backticks make template literals, which can embed values with ${...}.

Example

All three quote styles produce a string. The template literal in sentence inserts fullName and the result of age + 1.

Prompt
Show the string type with double quotes, single quotes and a template literal. Declare color ("blue"), fullName ('John Doe'), a number variable age (30), and a string variable sentence built with a template literal that inserts fullName and age + 1. Nothing is printed.
Output: TypeScript file and captured output

BigInt (ES2020+)

A bigint holds whole numbers larger than 2^53 - 1, which is the biggest whole number a number can represent exactly.

Example

BigInt(...) creates a bigint value, and TypeScript infers that type for hugeNumber.

Prompt
Show how to create a bigint value for whole numbers larger than 2^53 - 1. Declare a constant hugeNumber by calling BigInt(9007199254740991) and let TypeScript infer its type. Nothing is printed.
Output: TypeScript file and captured output

Symbol

A symbol is a unique identifier. Two symbols are never equal, even if they were created with the same description.

Symbols are useful for unique property keys and constants.

Example

uniqueKey is a symbol used as a computed property key in obj. Reading obj[uniqueKey] prints the stored text.

Prompt
Show the symbol type used as a unique object key. Create a constant uniqueKey typed as symbol from Symbol('description'), build an object obj whose computed key is uniqueKey with the value 'This is a unique property', then log obj[uniqueKey].
Output: TypeScript file and captured output