Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Strings & text

TypeScript docs MDN String ↗

Serialize a value as JSON

TypeScript

JSON.stringify encodes supported JavaScript values. It can throw for cycles/BigInt and omits or transforms some values; do not assume arbitrary object identity survives a round trip.

Fragment

Use a schema/encoding policy for dates, BigInt and other domain types.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const json = JSON.stringify({ name: "Ada", active: true });

Validate incoming JSON before using it

TypeScript

A type annotation or cast does not validate JSON. Check the actual shape and application constraints before consuming a field.

Fragment

Reject/handle additional fields according to your contract; use a named schema validator for larger schemas.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const value: unknown = JSON.parse(text);
if (typeof value !== "object" || value === null ||
    !("name" in value) || typeof value.name !== "string" ||
    value.name.trim() === "") {
  throw new Error("Expected a nonempty name");
}
const name = value.name;

Inspect, split and slice text

TypeScript

trim returns text without edge whitespace. split returns an array; map transforms each item. slice takes an end-exclusive range. length and slice use UTF-16 code units.

Fragment

Independent fragments.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const fields = "2 5".split(" ").map(Number);
const word = "north".slice(0, 2);
const clean = " west ".trim();

Read text and character literals

TypeScript

Single and double quotes create strings; there is no separate char primitive. Backticks allow interpolation and literal newlines. Escape sequences such as \n represent a character rather than two visible letters.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const name = "north";
const message = `hello ${name}`;
const newline = "\n";

Distinguish bytes, code points and characters

TypeScript

string.length counts UTF-16 code units. String iteration visits Unicode code points, not grapheme clusters. A visible character can contain multiple code points, so neither count is universally a user-perceived character count.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const text = "🦀";
const units = text.length; // 2
const points = [...text].length; // 1

Append and replace text

TypeScript

JavaScript string values are immutable. Concatenation/replacement returns another string; assigning it updates the binding. replace with a plain string replaces only the first match; replaceAll replaces all matches.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let text = "north";
text += " gate";

const changed = text.replace("north", "west");

Parse structured JSON with a named implementation

TypeScript

JSON.parse produces JavaScript values; TypeScript types do not validate the result. Validate required fields before use.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const value: unknown = JSON.parse(source);
if (typeof value !== "object" || value === null) {
  throw new Error("Expected an object");
}