๐ŸŽ“ A1Learner
๐Ÿ”

TypeScript Basic Types

The basic types name the kinds of value a variable can hold. The three main types are string, number, and boolean.

The three basic types

Each variable below carries a different basic type.

let city: string = "Oslo";
let age: number = 36;
let member: boolean = true;
console.log(city);
console.log(age);
console.log(member);

A boolean in a number variable

A boolean value does not fit a number variable. The compiler refuses the line.

let count: number = 5;
count = true;
console.log(count);

A boolean value changes

A boolean variable can move from false to true. Both values fit the type.

let ready: boolean = false;
ready = true;
console.log(ready);

Example

let fullName: string = "Ada Lovelace";
let age: number = 36;
let member: boolean = true;
console.log(`${fullName}, ${age}, member: ${member}`);

Key points

  • string holds text values.
  • number holds numeric values.
  • boolean holds true or false.
  • An annotation fixes the kind of value a variable can hold.
  • bigint holds whole numbers too large for number.
  • symbol creates a unique value.

In this example, fullName holds text and age holds a number. The member variable holds true. The log line joins all three values into one line of text.

Note

TypeScript has no separate type for whole numbers and decimals.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท TypeScript Playground

Exercise

Which type holds the values true and false?