Understanding Variables and Data Types in JavaScript

What is a variable and why we need them
A variable is a named container for a value. Without variables your code would be one long list of numbers and words you couldn’t refer to again. With variables you can:
- store user input (name, age)
- compute results and keep them
- make your code readable and maintainable
Real-life analogy: label a box age and put a number in it. Later you can open the box, change the number, or use it in calculations.
How to declare variables: var, let, const
Short summary:
var— old style, function-scoped, avoid for beginners.let— modern, block-scoped, you can change its value (reassign).const— modern, block-scoped, value cannot be reassigned (the variable binding is constant).
Examples:
// var (older style — function scoped)
var name = "Madhukar";
console.log(name); // Madhukar
// let (block scoped, reassignable)
let age = 20;
console.log(age); // 20
age = 21;
console.log(age); // 21
// const (block scoped, not reassignable)
const isStudent = true;
console.log(isStudent); // true
// isStudent = false; // Error: Assignment to constant variable.