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.

Print & read input

Print a value or a line

TypeScript

console.log appends a newline and formats values for inspection. stdout.write writes the supplied string without adding a newline.

Fragment

console is built in. process.stdout is Node-specific; browser console output is a developer tool rather than page UI.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
console.log("Hello");
console.log({ count: 3 });
process.stdout.write("progress...");

Write diagnostics to stderr

TypeScript

Keep diagnostics on stderr when stdout is structured output or a pipeline’s data stream.

Fragment

Node program. Setting exitCode allows normal pending cleanup/work to finish.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
console.error("Could not load settings");
process.exitCode = 1;

Log a message with useful context

TypeScript

console can log a message plus an object. A structured logger is a separate choice when you need stable JSON records, levels or destinations.

Fragment

Do not put credentials or private payloads in logs. Node console formatting is not a stable JSON serialization contract.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
console.info("item saved", { itemId: 24, elapsedMs: 8 });

Print or build text

TypeScript

console.log appends a newline. stdout.write writes the supplied text without adding one. String converts to text; JSON.stringify quotes/escapes a string as JSON.

Fragment

Complete Node module.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { stdout } from "node:process";

console.log("north");
stdout.write(JSON.stringify("a\tb"));

Recognize formatting controls

TypeScript

toFixed chooses decimal places and returns text. padStart pads text to a minimum length without truncating longer text. These operations can round or pad; they do not change the original number.

Fragment

Independent formatting fragments.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const price = (6.2).toFixed(1);
const tag = "x".padStart(4, ".");

Read a line

TypeScript

readline produces lines without their endings. crlfDelay: Infinity treats CRLF as one ending even when delayed. Async iteration waits for each available line.

Fragment

Complete Node module.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { createInterface } from "node:readline";
import { stdin } from "node:process";

async function show() {
  const lines = createInterface({
    input: stdin,
    crlfDelay: Infinity
  });

  for await (const line of lines) {
    console.log(line);
  }
}

void show();

Convert text to a number

TypeScript

Number converts a whole string using JavaScript numeric syntax; blank/whitespace becomes zero and invalid text becomes NaN. parseInt can accept a numeric prefix. isInteger tests the resulting number.

Fragment

Independent conversion fragment; no lab policy.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const value = Number("24");
const failed = Number.isNaN(value);
const whole = Number.isInteger(value);

Recognize raw input scaffolding

TypeScript

setEncoding makes stdin yield decoded strings. Stream blocks are not guaranteed to be lines. A decoder/line accumulator must preserve partial records across chunks.

Fragment

Complete program; observes sizes, assembles no lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { stdin } from "node:process";

async function sizes() {
  stdin.setEncoding("utf8");
  for await (const block of stdin) {
    console.log(String(block).length);
  }
}
void sizes();