Understand the idea
A variable binding lets you refer to a value by name. Use const when the binding will not be reassigned, and let when reassignment is part of the logic.
JavaScript values have types such as string, number and Boolean. A const binding cannot be assigned a different value later, but an object stored in it can still be modified. Prefer descriptive names over unexplained single letters.
Read the example
This is a JavaScript fragment. Run it in a browser console or an external script. Supply any HTML or data file named in the example first.
const projectName = "My gallery";
let photographCount = 12;
photographCount += 1;
console.log(projectName, photographCount);A small mistake, explained
What goes wrong
The + operator can join strings. "12" + 1 produces "121", not 13.
How to fix it. Inspect the type with typeof. Convert a string with Number when a number is required, and handle invalid conversions.
Try it yourself
Run the code in the browser console. Replace 12 with "12" and explain the changed result before fixing it.
Further reading
Keep exploring
- JavaScript · Guide
Conditions & comparisonsMake a decision without accidentally changing the value.
- JavaScript · Guide
Functions & return valuesTurn a small task into a named operation.