JavaScript Series — Phase 1 day 3 — JavaScript Fundamentals
📅 Day 3 — Variables in JavaScript
1. 📦 What is a Variable?
A variable is a named container used to store data.
let name = "Tuhin";
let age = 20;
console.log(name);
console.log(age);
You can think of it as:
name → "Tuhin"
age → 20
2. 🔑 var, let, and const
JavaScript provides three keywords for declaring variables.
var city = "Kolkata";
let age = 20;
const country = "India";
var
Older way of declaring variables
Function-scoped
Can be redeclared and reassigned
let
Modern way to declare a variable
Block-scoped
Can be reassigned
Cannot be redeclared in the same scope
const
Block-scoped
Cannot be reassigned
Cannot be redeclared
Used when the variable reference should remain constant
3. ✏️ Variable Naming Rules
JavaScript variable names have some important rules:
let firstName = "Tuhin";
let user_age = 20;
let $price = 500;
let _value = 100;
Rules:
Can contain letters, numbers,
_, and$Cannot start with a number
Cannot contain spaces
Cannot use reserved JavaScript keywords
JavaScript is case-sensitive
❌ Invalid:
let 1name = "Tuhin";
let user name = "Tuhin";
✅ Valid:
let name1 = "Tuhin";
let userName = "Tuhin";
A common convention is camelCase:
let firstName = "Tuhin";
let totalAmount = 5000;
4. 🔄 Reassigning Variables
Reassignment means giving an existing variable a new value.
With let:
let age = 20;
age = 21;
console.log(age);
Output:
21
With const:
const country = "India";
country = "Japan"; // Error
So:
let → can be reassigned
const → cannot be reassigned
5. ⚖️ let vs const vs var
| Feature | var |
let |
const |
|---|---|---|---|
| Reassign | ✅ | ✅ | ❌ |
| Redeclare | ✅ | ❌ | ❌ |
| Block scoped | ❌ | ✅ | ✅ |
| Modern usage | ⚠️ Avoid generally | ✅ | ✅ |
| Common use | Legacy code | Changing values | Fixed references |
⭐ Best Practice
For modern JavaScript:
const name = "Tuhin";
let age = 20;
Use const by default, and use let when you know the value needs to change.
Generally, avoid var in new code unless you specifically need its older scoping behavior.
Comments
Join the discussionNo comments yet. Be the first to comment.