A variable is a named box that stores a value.
Three keywords
| Keyword | Can change? | Use it |
|---|---|---|
let |
yes | for values that change |
const |
no (cannot be reassigned) | by default, for values that stay the same |
var |
yes | old style — avoid in new code |
const name = 'Asha';
let age = 21;
age = 22; // allowed
Naming rules
- Start with a letter,
_or$ - No spaces; use camelCase:
userName - Cannot be a keyword like
letorif
Tip
Use const first. Switch to let only when the value must change.
Example
Example — let and const
HTML
<p id="out"></p>
<script>
const city = 'Gaya';
let score = 10;
score = score + 5;
document.getElementById('out').textContent = city + ' – score: ' + score;
// city = 'Patna'; // Error: const cannot be reassigned
</script>