# Primitive Types

> TypeScript builds on JavaScript's primitive types by allowing you to explicitly declare which type a variable holds. The seven primitive types in...

Canonical: https://www.internhack.in/learn/typescript/ts-basics/primitive-types
Last updated: 2026-11-28

Difficulty: Beginner

TypeScript builds on JavaScript's primitive types by allowing you to explicitly declare which type a variable holds. The seven primitive types in TypeScript mirror JavaScript: \`string\`, \`number\`, \`boolean\`, \`null\`, \`undefined\`, \`bigint\`, and \`symbol\`. When you annotate a variable with a primitive type, TypeScript ensures that only values of that type can be assigned to it throughout its lifecycle.

The \`string\` type covers all text values, whether created with single quotes, double quotes, or template literals. The \`number\` type encompasses all numeric values - integers, floats, \`Infinity\`, and \`NaN\` (yes, NaN is of type number). The \`boolean\` type is limited to \`true\` and \`false\`. These three are the most commonly used primitives in day-to-day TypeScript development.

The \`null\` and \`undefined\` types are special. With \`strictNullChecks\` enabled (which is the default under \`strict: true\`), \`null\` and \`undefined\` are their own distinct types and cannot be assigned to other types without an explicit union. This is one of TypeScript's most valuable features: it forces you to handle cases where a value might not exist, preventing the infamous "Cannot read property of null" errors.

The \`bigint\` type represents arbitrarily large integers (created with the \`BigInt()\` function or the \`n\` suffix like \`100n\`). The \`symbol\` type represents unique identifiers created with \`Symbol()\`. Both are less commonly used but important in specific contexts - \`bigint\` for large number arithmetic and \`symbol\` for creating unique object property keys that won't collide with other properties.

## Code examples

### Basic primitive type annotations

```
const name: string = "TypeScript";
const version: number = 5.3;
const isTyped: boolean = true;
const nothing: null = null;
const notDefined: undefined = undefined;

console.log(`${name} ${version}, typed: ${isTyped}`);
console.log(`null is: ${nothing}, undefined is: ${notDefined}`);
```

Each variable is annotated with its primitive type. TypeScript will prevent you from reassigning a value of a different type to any of these variables.

### Number edge cases and bigint

```
const integer: number = 42;
const float: number = 3.14;
const negative: number = -10;
const notANumber: number = NaN; // NaN is still type 'number'
const infinite: number = Infinity;

console.log(typeof notANumber); // "number"
console.log(Number.isNaN(notANumber)); // true

const bigValue: bigint = 9007199254740993n;
console.log(bigValue.toString());
```

NaN and Infinity are both valid number values in TypeScript. BigInt handles integers larger than Number.MAX_SAFE_INTEGER accurately.

### Strict null checks in action

```
function findUser(id: number): string | null {
  if (id === 1) return "Alice";
  return null;
}

const user = findUser(1);
const missing = findUser(99);

// Must check for null before using string methods
if (user !== null) {
  console.log(user.toUpperCase());
}
console.log(missing === null ? "No user found" : missing);
```

With strictNullChecks, the return type \`string | null\` forces callers to handle the null case. This prevents runtime null reference errors.

## Key points

- Always use lowercase primitive types (\`string\`, \`number\`, \`boolean\`) in annotations - not the uppercase wrapper objects (\`String\`, \`Number\`, \`Boolean\`).
- With strict null checks enabled, \`null\` and \`undefined\` are not assignable to other types. Use union types like \`string | null\` when a value can be absent.
- The \`typeof\` operator works at runtime in JavaScript. TypeScript also uses it in type positions: \`type T = typeof myVariable\`.

## Concepts covered

string, number, boolean, null, undefined, bigint, symbol

Previous: [Setting Up TypeScript](https://www.internhack.in/learn/typescript/ts-basics/setting-up-typescript) · Next: [Type Inference](https://www.internhack.in/learn/typescript/ts-basics/type-inference) · [All of TypeScript Basics](https://www.internhack.in/learn/typescript/ts-basics)

## More TypeScript Basics lessons

- [Arrays and Tuples](https://www.internhack.in/learn/typescript/ts-basics/arrays-and-tuples)
- [What is TypeScript?](https://www.internhack.in/learn/typescript/ts-basics/what-is-typescript)

## Related

- [Learn](https://www.internhack.in/learn)
- [TypeScript](https://www.internhack.in/learn/typescript)
- [TypeScript Basics](https://www.internhack.in/learn/typescript/ts-basics)
