Variables (let, const & var) & Scopes
Welcome to Lesson 3 of Phase 2: Variables & Data Types! In JavaScript, data is stored and manipulated through Variables. ES6 (2015) introduced modern let and const keywords to replace legacy var. In this comprehensive lesson, you will learn what variables are, the critical differences between let, const, and var, value mutation vs reference mutation, variable naming conventions, and JavaScript scope mechanics.
Variable anedhi JavaScript engine memory (RAM) lo values ni store chesukovadaniki allocate chesina oka Named Storage Box (Memory Container). Program execution jarige time lo values ni retain cheyyadaniki mariyu mutate (vary) cheyyadaniki variables ni vadathamu.
keyword variableName = value;
JavaScript lo variables declare cheyyadaniki 3 keywords unnaayi: let, const, and legacy var:
| Feature | let (Modern ES6) | const (Modern ES6) | var (Legacy ES5) |
|---|---|---|---|
| Scope Type | Block Scope { } |
Block Scope { } |
Function / Global Scope |
| Re-assignable? | โ Yes | โ No (Read-only reference) | โ Yes |
| Re-declarable? | โ No | โ No | โ Yes (Dangerous!) |
| Hoisting & TDZ | Hoisted in Temporal Dead Zone (Cannot access before declaration) | Hoisted in Temporal Dead Zone (Cannot access before declaration) | Hoisted with undefined default value |
| Initial Value Required? | No (defaults to undefined) |
โ Must initialize immediately | No (defaults to undefined) |
"use strict";
// 1. let โ Block-scoped & Reassignable
let age = 21;
age = 22; // Reassignment allowed!
console.log("Updated Age:", age);
// 2. const โ Block-scoped Constant Reference
const name = "Ravi";
// name = "Kalyan"; // TypeError: Assignment to constant variable!
console.log("Name:", name);
// 3. var โ Legacy function-scoped variable
var city = "Hyderabad";
var city = "Bangalore"; // Allowed re-declaration (Bad practice!)
console.log("City:", city);
const primitive value ni re-assign cheyyanivvadu. Kani const tho declare chesina Object or Array properties ni mutate (change) cheyyavachu. Endhukante const memory address reference ni lock chesthundi, internal heap data ni kaadhu:
"use strict";
const student = {
name: "Ravi",
age: 21
};
// Modifying object properties IS ALLOWED:
student.age = 22;
student.city = "Hyderabad";
console.log("Mutated Student:", student);
// RE-ASSIGNING THE ENTIRE OBJECT IS FORBIDDEN:
// student = { name: "Kalyan" }; // TypeError!
- Allowed Characters: Letters (
a-z, A-Z), Digits (0-9), Dollar Sign ($), and Underscore (_). - Forbidden: Cannot start with a digit (e.g.
1stUseris illegal,user1is legal). - Forbidden: Reserved JavaScript keywords (e.g.
let,class,function,if). - Case Sensitivity:
userAge,UserAge,USERAGEare 3 completely separate variables. - Industry Convention: Always use camelCase for variable and function names (e.g.
isStudent,accountBalance).
Scope defines the accessibility (visibility) of variables in your code:
Anywhere outside functions/blocks. Accessible everywhere in the file.
Declared inside a function() { }. Accessible only inside that function.
Declared inside { } using let or const. Destroyed on block exit.
Run this variable profile example from the curriculum:
"use strict";
const name = "Ravi";
let age = 21;
const isStudent = true;
console.log("Name:", name);
console.log("Age:", age);
console.log("Is Student:", isStudent);
console.log("Type of Age:", typeof age);