Think of a variable as a labeled box where you can store information.
In JavaScript, variables are containers for storing data values like numbers, strings, objects, etc.
To use a variable, you must first declare it using one of the keywords: var,
let, or const.
2. Variable Declarations
var – The old way (ES5). Function-scoped.
let – The modern way (ES6). Block-scoped and reassignable.
const – The modern way (ES6). Block-scoped and constant (cannot be reassigned).
A. The `var` Keyword
var was the original way to declare variables. It is function-scoped (or globally scoped
if declared outside a function).
It can be re-declared and updated.
var x = 10;
var x = 20; // Re-declaration allowed
x = 30; // Update allowed
console.log("Value of x:", x);
Output will appear here...
B. The `let` Keyword
let was introduced in ES6 (2015). It is block-scoped (only exists within the
{} it is declared in).
It cannot be re-declared in the same scope, but it can be updated.
let y = 10;
// let y = 20; // Error: Identifier 'y' has already been declared
y = 20; // Update allowed
if (true) {
let y = 50; // This is a different 'y' inside this block
console.log("Inside block:", y);
}
console.log("Outside block:", y);
Output will appear here...
C. The `const` Keyword
const is also block-scoped. However, variables declared with const cannot
be reassigned.
You must initialize it with a value when you declare it.
const country = "India";
// country = "USA"; // Error: Assignment to constant variable
console.log("Country:", country);
// Note: Objects declared with const can still be mutated
const person = { name: "Ragini" };
person.name = "Ravi"; // This is allowed!
console.log("Person Name:", person.name);
Output will appear here...
3. Scope Explained
Scope determines the visibility of variables.
Global Scope: Variables declared outside any function. Accessible everywhere.
Function Scope: Variables declared inside a function. Accessible only inside that function
(applies to var, let, const).
Block Scope: Variables declared inside a block {}. Accessible only inside that block
(applies to let, const).
4. Comparison Table
Feature
var
let
const
Scope
Function Scope
Block Scope
Block Scope
Re-declare
✅ Yes
❌ No
❌ No
Re-assign
✅ Yes
✅ Yes
❌ No
Hoisting
Yes (initialized as undefined)
Yes (but in Temporal Dead Zone)
Yes (but in Temporal Dead Zone)
Practice Questions
Test your understanding of JavaScript Variables.
Easy
Q1: Store student name, roll number, and course. Print all details.
The let or const keyword is used to store variables. You can then print them using console.log().
let name = "Ravi";
let rollNo = 101;
let course = "BCA";
console.log("Name:", name);
console.log("Roll No:", rollNo);
console.log("Course:", course);
Easy
Q2: Store a product name, price, and quantity in variables. Store the total cost directly in a new variable.
Declare the variables using let or const and assign the appropriate string and numeric values.
let productName = "Shoes";
let price = 1000;
let quantity = 2;
let totalCost = 2000; // stored directly
console.log("Product:", productName);
console.log("Total Cost:", totalCost);
Medium
Q3: Store an initial bank balance and a deposit amount in variables. Store the final balance directly in a third variable.
Create variables for the numbers and output the final value. It is best practice to use let if values might change, or const if they won't.
let balance = 5000;
let deposit = 2000;
let finalBalance = 7000; // stored directly
console.log("Final Balance:", finalBalance);
Medium
Q4: Store a person's name, age, and city in variables. Combine them to store a full sentence in one single variable.
You can combine strings and variables using the + operator or ES6 Template Literals (backticks).
let name = "Ragini";
let age = 20;
let city = "Delhi";
let sentence = name + " is " + age + " years old and lives in " + city + ".";
// OR: let sentence = `${name} is ${age} years old and lives in ${city}.`;
console.log(sentence);
Hard
Q5: Create a program to print a full report card using variables.
Store: Student Name, Roll Number, Class
Store: 5 Subject Names, 5 Subject Marks
Store: Total Marks & Percentage (calculate manually and assign directly)
Store: Final Result sentence in one variable
Declare all the required variables step-by-step. Group them logically, then use document.write() to print out a nicely formatted report card.
let studentName = "Ragini";
let rollNo = 101;
let studentClass = "10th";
// Subject Names
let sub1 = "Math";
let sub2 = "Science";
let sub3 = "English";
let sub4 = "Hindi";
let sub5 = "Computer";
// Marks
let marks1 = 85;
let marks2 = 90;
let marks3 = 88;
let marks4 = 92;
let marks5 = 95;
// Already calculated values
let totalMarks = 450;
let percentage = 90;
// Final Result
let result = "Ragini has passed with 90% marks";
// Output
document.write("----- Report Card -----");
document.write("Name:", studentName);
document.write("Roll No:", rollNo);
document.write("Class:", studentClass);
document.write("-----------------------");
document.write(sub1 + ": " + marks1);
document.write(sub2 + ": " + marks2);
document.write(sub3 + ": " + marks3);
document.write(sub4 + ": " + marks4);
document.write(sub5 + ": " + marks5);
document.write("-----------------------");
document.write("Total:", totalMarks);
document.write("Percentage:", percentage + "%");
document.write("Result:", result);