Variables (let, const)

Duration: 10 min  •  Difficulty: Easy

In modern JavaScript (ES6+), there are two main ways to declare variables: let and const. Please avoid using var as it is prone to bugs.

1. let

Use let if you know that the value inside that variable will change (re-assigned) in the future.

2. const

Short for *constant*. Use const if the value will NOT change (and should not be changed) after it is created. Best practice in JS: Always use const first. Change to let ONLY if you realize it needs heart-dynamic nature.

main.js
// Using const (Fixed value)
const birthYear = 2000;
// birthYear = 2001; TypeError!
// Using let (Can be changed)
let age = 22;
age = 23; // Allowed
age = age + 1; // Becomes 24
console.log("Current age:", age);