JavaScript
Chapter 1: Variables & Declarations
Understand variables in JavaScript, including var, let, const, block scope, function scope, and hoisting.
What are Variables?
- Variables are containers that hold data.
- They help us store, reuse, and update information in JavaScript — from simple values like numbers to complex data like arrays and objects.
- Think of a variable as a box with a name on it. You can put something inside it (a value), and later check or change what's inside.
- In JavaScript, you create these boxes using keywords:
var,let, orconst.
var, let, and const
var
- Old and Risky
- Scoped to functions, not blocks
- Can be redeclared and reassigned
- Hoisted to the top with
undefinedvalue
var score = 10;
var score = 20; // OK
let
- Modern & Safe
- Scoped to blocks (
{}) - Can be reassigned but not redeclared
- Hoisted, but stays in the Temporal Dead Zone (TDZ)
let age = 25;
age = 30; // ✅
let age = 40; // ❌ Error (same block)
const
- Constant values
- Scoped to blocks
- Cannot be reassigned or redeclared
- Value must be assigned at declaration
- TDZ applies here too
const PI = 3.14;
PI = 3.14159; // ❌ Error
But: If const holds an object/array, you can still change its contents:
const student = { name: "Shoaib" };
student.name = "Harsh"; // ✅ OK
student = {}; // ❌ Error
Scope in Real Life
- Block Scope → Code inside
{}like in loops,if, etc. - Function Scope → Code inside a function
letandconstfollow block scope.varignores block scope — which leads to bugs.
{
var x = 5;
let y = 10;
const z = 15;
}
console.log(x); // ✅ 5
console.log(y); // ❌ ReferenceError
console.log(z); // ❌ ReferenceError
Hoisting
- JavaScript prepares memory before running code.
- It moves all declarations to the top — this is called hoisting.
But:
varis hoisted and set toundefined.letandconstare hoisted but not initialized — so accessing them early gives ReferenceError.
console.log(a); // undefined
var a = 10;
console.log(b); // ❌ ReferenceError
let b = 20;
Common Confusions
constdoesn't make things fully constant. It protects the variable, not the value.varis outdated — it's better to useletandconst.letandconstbehave similarly, butconstgives more safety — use it when you're not planning to reassign.
Mindset Rule
- Use
constby default. Useletonly when you plan to change the value. - Avoid
var— it belongs to the past.
Practice Zone
- Declare your name and city using
const, and your age usinglet. - Try this and observe the result:
let x = 5;
let x = 10;
- Guess the output:
console.log(count);
var count = 42;
- Create a
constobject and add a new key to it — does it work? - Try accessing a
letvariable before declaring it — what error do you see? - Change a
constarray by pushing a value. Will it throw an error?
Welcome
Skdev Online Academy is an academic & practical platform empowering learners to master programming and cybersecurity — Learn Beyond Limits.
2. Data Types + Type System
Learn about JavaScript data types, primitive vs reference types, typeof operator, coercion, truthy/falsy values, and type system concepts.
