JavaScript Variables
A variable is a named container that holds a value. Think of it as a labeled box — the label is the variable name, and the item inside the box is the value. Variables allow programs to store, retrieve, and update data during execution.
In JavaScript, there are three ways to declare a variable: var, let, and const. Each has its own behavior and use case.
Declaring a Variable
To declare (create) a variable, use one of the keywords followed by a name:
let city = "Mumbai";
console.log(city); // MumbaiHere, let is the keyword, city is the variable name, and "Mumbai" is the value assigned to it using the = sign.
The var Keyword
var is the original way to declare variables in JavaScript (before ES6 in 2015). It still works, but it has some quirky behaviors that can cause bugs. Modern JavaScript code avoids var and uses let or const instead.
var country = "India";
console.log(country); // India
// var can be re-declared in the same scope (this can cause bugs)
var country = "Nepal";
console.log(country); // NepalCharacteristics of var
- Function-scoped (accessible anywhere within the function it is declared in)
- Can be re-declared in the same scope
- Gets hoisted to the top of its scope (more on hoisting later)
The let Keyword
let was introduced in ES6 and is the recommended way to declare variables that will change in value. It fixes many issues with var.
let score = 10;
console.log(score); // 10
score = 20; // Value can be updated
console.log(score); // 20Characteristics of let
- Block-scoped (only accessible within the block
{ }it is declared in) - Cannot be re-declared in the same scope
- Can be updated (reassigned)
let age = 25;
// let age = 30; // Error: Cannot redeclare 'age'
age = 30; // This is fine — updating the value
console.log(age); // 30The const Keyword
const is used to declare variables whose values should never change. Once a value is assigned to a const variable, it cannot be updated or re-declared.
const PI = 3.14159;
console.log(PI); // 3.14159
// PI = 3.14; // Error: Assignment to constant variableCharacteristics of const
- Block-scoped
- Cannot be updated or re-declared
- Must be assigned a value at the time of declaration
// const must be initialized immediately
const TAX_RATE = 0.18;
// This is not allowed:
// const TAX_RATE; // Error: Missing initializer in const declarationvar vs let vs const — Comparison Table
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Re-declare | Yes | No | No |
| Reassign | Yes | Yes | No |
| Hoisting | Yes (undefined) | Yes (not initialized) | Yes (not initialized) |
| Use today? | Avoid | Yes | Yes (preferred) |
Variable Assignment
Variables can be declared without a value first, and assigned later. This only works with let and var.
let username; // Declared, no value yet
console.log(username); // undefined
username = "Rohit"; // Value assigned later
console.log(username); // RohitMultiple Variable Declarations
Multiple variables can be declared on the same line by separating them with commas:
let a = 5, b = 10, c = 15;
console.log(a + b + c); // 30Changing Variable Values
let temperature = 30;
console.log("Morning temperature:", temperature); // 30
temperature = 35;
console.log("Afternoon temperature:", temperature); // 35
temperature = 28;
console.log("Evening temperature:", temperature); // 28const with Objects and Arrays
An important detail about const: when used with objects and arrays, the variable reference cannot change, but the contents of the object or array can still be modified.
const person = { name: "Anil", age: 22 };
// Changing a property is allowed
person.age = 23;
console.log(person.age); // 23
// Reassigning the entire object is NOT allowed
// person = { name: "Suresh" }; // Error!Practical Example
// Store a student's details
const studentId = 1001; // Fixed — never changes
let studentName = "Kavita"; // Can change if student updates name
let totalMarks = 0; // Will be updated as marks are added
totalMarks = totalMarks + 85; // Subject 1
totalMarks = totalMarks + 90; // Subject 2
totalMarks = totalMarks + 78; // Subject 3
console.log("Student:", studentName);
console.log("Total Marks:", totalMarks); // 253Key Points to Remember
- Variables are named containers that hold values
- Use
constby default — switch toletonly when the value needs to change - Avoid
varin modern JavaScript letandconstare block-scoped — they only exist within their surrounding{ }- A variable declared without a value holds
undefinedby default constobjects and arrays can have their contents modified, just not replaced entirely
