Course curriculum
JavaScript · LESSON 2 OF 5

Values, const and let

Choose between const and let, distinguish strings from numbers and compare values predictably.

About 9 minutes · Beginner

Choose a declaration

Use const when a binding will not be reassigned. Use let when you intend to assign it a new value. Clear names make each value’s purpose visible.

const does not make an object deeply immutable: properties can still change. It prevents replacing the binding itself.

JavaScript
const totalLessons = 5;
let completedLessons = 1;
completedLessons += 1;
console.log(totalLessons - completedLessons);
Expected output
3

Avoid accidental string joining

The + operator joins strings when a string operand is involved. Number converts a numeric string for arithmetic; invalid numeric text produces NaN.

Use Number.isFinite after conversion when input must be a usable finite number. Empty strings convert to zero, so validate required input separately.

JavaScript
console.log("12" + 3);
console.log(Number("12") + 3);
console.log(Number.isFinite(Number("twelve")));
Expected output
123
15
false

Use strict comparisons

=== compares without converting the two values to a common type. It makes a string and a number with similar spelling remain distinct.

Template literals use backticks and ${...} placeholders to combine values with explanatory text.

JavaScript
const score = 42;
console.log(score === "42");
console.log(`Score: ${score}`);
Expected output
false
Score: 42

Try it yourself

Declare a unit price of 25 and a quantity of 3. Print “Total: 75” using a template literal.

Show a possible solution
JavaScript
const price = 25;
const quantity = 3;
console.log(`Total: ${price * quantity}`);

Keep exploring: official JavaScript documentation.

0 of 5 completed on this device