Zum Inhalt springen
KodeTrail

Camp 2 · Schritt 4 von 18

let, const, and naming

Store values under names — and learn why modern JavaScript reaches for const first.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Variables let programs remember. Modern JavaScript gives you two keywords for creating them — and choosing between them is easier than it looks.

let: a box you can refill

JavaScript

let declares a variable. Later assignments (no let needed) replace its value. Watch the box refill:

Speicher-Labor0/3
let steps = 4200;
steps = steps + 800;
let trail = "python";

Variablen im Speicher

Noch nichts gespeichert — geh den Code Schritt für Schritt durch.

const: a box that's sealed

JavaScript

Run it: TypeError: Assignment to constant variable. A const can never be reassigned — and that's a feature. Most values in real programs never change after creation, and const makes that promise enforceable.

Naming: camelCase

JavaScript names use camelCase: first word lowercase, every following word capitalized — totalSteps, firstName, isLoggedIn. Rules: letters, digits, _, $; can't start with a digit; case-sensitive.

Checkpoint

Which line will throw an error?

const max = 100;
let count = 0;
count = 5;
max = 200;
Checkpoint

Which name follows JavaScript convention?

What's next

Numbers and math, JavaScript style — including one number type doing the work of two.