agyanadda@gmail.com

JS Variables

JavaScript Basics 1 min read Updated Dec 1, 2026 Topic 5 of 38

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 let or if

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>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which keyword creates a variable that cannot be reassigned?

Exercise 2 True / False

A variable name can start with a number.

Exercise 3 Fill in the Blank

Use ___ for a variable whose value will change later.

Frequently Asked Questions

Yes. const stops reassigning the variable, but the array or object it points to can still be changed (for example with push).