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.

Frequently asked questions

How does JavaScript handle primitive types?
This task is covered in 2 stacks on this page: JavaScript, TypeScript. The "Primitive Types" snippet in JavaScript uses `string | number | boolean | undefined | null | symbol | bigint`.
Which code does the JavaScript example use?
The "Primitive Types" snippet uses `string | number | boolean | undefined | null | symbol | bigint`, from the Data Types section of the JavaScript cheat sheet.
Which stacks cover "primitive types" on this page?
JavaScript, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Primitive Types": Primitives are immutable and compared by value. There are 7 primitive types in total.