Variables & Types
A variable is a labeled box. You put a value in it, you give the box a name, and later you refer to the box by name instead of retyping the value. That's the entire idea — everything else is just rules about the box.
💡 The Bottom Line
Use const by default, let when a value needs to change, and avoid
var in new code. JavaScript won't ask what type of value goes in a box — it figures
that out automatically, which is powerful and occasionally surprising.
Declaring a box
const name = "Deva"; // can't be reassigned
let score = 0; // can be reassigned
score = 10; // fine
name = "Someone else"; // ❌ error — const can't change
⚠️ Watch Out
You'll see a third keyword, var, in older code and tutorials. It works, but it has
confusing scoping rules that let and const were specifically created to
fix. Modern JavaScript treats var as legacy — reach for const first,
let second, and skip var entirely.
The types that fill the box
JavaScript is dynamically typed — a box doesn't have a fixed type; whatever you put in it decides the type at that moment. There are six simple ("primitive") types you'll use constantly:
let a = "Chennai"; // string — text, in quotes
let b = 42; // number — JS has just one number type
let c = true; // boolean — true or false
let d; // undefined — declared, no value yet
let e = null; // null — deliberately empty, on purpose
let f = { city: "Karaikkudi" }; // object — a labeled bundle of values
Ask JavaScript what type something is with typeof:
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" — a decades-old bug nobody could safely fix
🙋 There Are No Dumb Questions
Q: What's the actual difference between undefined and null?
A: undefined means "nobody has put anything here yet" — JavaScript's own default.
null means a person deliberately said "this is empty" on purpose. You'll mostly see
undefined happen by accident and null written on purpose.
Now, about that "2" + 2
Back in Lesson 2 you probably saw "2" + 2 print "22", not 4.
Here's why: + means addition between two numbers, but it means joining text
(concatenation) if either side is a string. JavaScript picks the second meaning the moment it sees a
string, instead of raising an error.
"2" + 2 // "22" (string wins, joins as text)
2 + 2 // 4 (both numbers, adds)
"5" - 1 // 4 (minus only means subtraction, so JS converts "5" to a number)
This automatic guessing is called type coercion. It's convenient until it isn't — experienced developers write code carefully around it rather than being surprised by it.
Template literals — readable strings
Backticks let you drop variables straight into a string instead of stitching pieces together with +:
const city = "Karaikkudi";
const years = 18;
// the old way
console.log("Deva has worked " + years + " years, based in " + city);
// template literal — cleaner
console.log(`Deva has worked ${years} years, based in ${city}`);
🔧 Try It Yourself
Open your console and predict the output before running each line:
let x = "10";
let y = 5;
console.log(x + y);
console.log(x - y);
console.log(typeof (x + y));
console.log(typeof (x - y));
✅ Quick Check
Which variable keyword should you reach for by default in new code?
Boxes are only useful if you can act on what's inside them. Lesson 4 is where JavaScript starts doing things — functions.