Variables (let, const & var) & Scopes

๐ŸŸจ JavaScript (ES2026+) ๐ŸŸข Lesson 3 ๐Ÿ“‚ Phase 02: Variables & Data Types ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this lesson: Variables ante enti? ยท let ยท const ยท var ยท Reassigning Values ยท Variable Naming Rules ยท Global vs Function vs Block Scope ยท TDZ

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.

1Variables Ante Enti? (What is a Variable?)

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.

๐Ÿ“Œ Variable Syntax: keyword variableName = value;
2The Big 3: let vs const vs var

JavaScript lo variables declare cheyyadaniki 3 keywords unnaayi: let, const, and legacy var:

Featurelet (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)
JavaScript โ€” let vs const vs var โ–ถ Run Code
"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);
3Reassigning Values vs Constant Objects

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:

JavaScript โ€” const Object Mutation โ–ถ Run Code
"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!
4JavaScript Variable Naming Rules
  • Allowed Characters: Letters (a-z, A-Z), Digits (0-9), Dollar Sign ($), and Underscore (_).
  • Forbidden: Cannot start with a digit (e.g. 1stUser is illegal, user1 is legal).
  • Forbidden: Reserved JavaScript keywords (e.g. let, class, function, if).
  • Case Sensitivity: userAge, UserAge, USERAGE are 3 completely separate variables.
  • Industry Convention: Always use camelCase for variable and function names (e.g. isStudent, accountBalance).
5Scope Basics (Global, Function & Block Scope)

Scope defines the accessibility (visibility) of variables in your code:

1. Global Scope

Anywhere outside functions/blocks. Accessible everywhere in the file.

2. Function Scope

Declared inside a function() { }. Accessible only inside that function.

3. Block Scope { }

Declared inside { } using let or const. Destroyed on block exit.

๐Ÿ’ป Try It Yourself โ€” User Example

Run this variable profile example from the curriculum:

JavaScript โ–ถ Run Code
"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);
Run in Our Compiler โ†’
OC
Written and reviewed by Our Compiler Technical Team ยท Updated for JavaScript ES2026+