Primitive types

2 snippets across 2 stacks — JavaScript, TypeScript

JSJavaScript

Primitive Types

JS · Data Types
syntax
string | number | boolean | undefined | null | symbol | bigint
example
const name = "Mira";       // string
const age = 28;            // number
const active = true;       // boolean
const missing = undefined; // undefined
const empty = null;        // null
const id = Symbol("id");   // symbol
const big = 900719925474099267n; // bigint

Note Primitives are immutable and compared by value. There are 7 primitive types in total.

TSTypeScript

string, number, boolean

TS · Basic Types
syntax
let varName: string = value;
let varName: number = value;
let varName: boolean = value;
example
let username: string = "alice";
let price: number = 29.99;
let isActive: boolean = true;
output
// All three are primitive types inferred automatically when initialized

Note Use lowercase string, number, boolean. The uppercase versions (String, Number, Boolean) are wrapper objects and almost never what you want.