JSJavaScript

JavaScript: Basics – code examples

Contents

Hello World

Basics#console#outputCompare in all languages
Use case: A first test that the browser console or Node works.
console.log("Hello World!");

console.log writes to the console – no main, no class needed.

Variables and types

Basics#const#let#typeof#variableCompare in all languages
Use case: Store values and check their type.
const name = "Ada";
let alter = 36;
alter += 1;
const groesse = 1.68;
const aktiv = true;
console.log(name, alter, groesse, aktiv);
console.log(typeof alter, typeof groesse, typeof aktiv);

const cannot be reassigned, let can. Integers and decimals are both number; typeof shows the type.

Date and time

Basics#date#time#datetime#intl
Use case: Calculate deadlines, format timestamps.
const jetzt = new Date(2026, 2, 15, 9, 30);   // months count from 0!
console.log(jetzt.toLocaleDateString("de-DE", { day: "2-digit", month: "2-digit", year: "numeric" }));
const spaeter = new Date(jetzt);
spaeter.setDate(spaeter.getDate() + 30);
console.log(spaeter.getMonth() + 1, spaeter.getDate());
const tage = Math.round((new Date(2026, 11, 24) - new Date(2026, 2, 15)) / 86400000);
console.log(tage, "days until Christmas");

The month starts at 0 (March = 2). The difference between two dates is in milliseconds. For complex time-zone logic you use Intl or libraries (soon Temporal).

Formatting numbers and dates (Intl)

Basics#intl#format#locale#currency
Use case: Display prices, numbers and dates in the local style.
const betrag = 1234567.891;
console.log(betrag.toLocaleString("de-DE", { style: "currency", currency: "EUR" }));
console.log(new Intl.NumberFormat("en-US").format(betrag));
const datum = new Date(Date.UTC(2026, 2, 15));
console.log(new Intl.DateTimeFormat("de-DE", { dateStyle: "long", timeZone: "UTC" }).format(datum));

The built-in Intl API formats by language and region – no library needed. Expected output: 1.234.567,89 €, 1,234,567.891, 15. März 2026.