Variables, types, and the == trap that has bitten everyone
Variables in JavaScript look simple. They are not. There are three keywords, seven primitive types, two ways to compare values, and one famous bug that costs juniors their afternoons. Let us clear all of it in one short lesson.
The good news: in 2026 the rules are settled. You only really need two keywords and one comparison operator.
let, const, and the ghost of var
constis your default. Use it for everything until the value actually needs to change.letis for values that will be reassigned. Loop counters, accumulators, swapping state.varis legacy. It is function-scoped (not block-scoped) and gets hoisted in confusing ways. Do not use it in new code.
const userName = 'Priya';
let orderCount = 0;
orderCount = orderCount + 1;If your linter ever complains about
var, it is doing you a favour. Trust it.
The seven primitive types
JavaScript has seven primitives. That is the whole list.
stringfor text:'Bengaluru'numberfor both integers and decimals:42,3.14booleanfortrue/falseundefinedwhen a variable is declared but not setnullwhen you explicitly mean "nothing here"bigintfor huge integers, written9007199254740993nsymbolfor unique keys you will rarely create yourself
Everything else (arrays, functions, dates, regex) is an object.
typeof and its one famous bug
typeof tells you the type of a value. It works as expected for most things, with one historical bug nobody fixed because too much code depends on it.
typeof 'hello'; // 'string'
typeof 42; // 'number'
typeof undefined; // 'undefined'
typeof null; // 'object' the bug
typeof []; // 'object' (arrays are objects)To check for an array, use Array.isArray(value). To check for null, compare directly: value === null.
The == trap
JavaScript has two equality operators. == does type coercion, which means it tries to be helpful and ends up lying to you.
0 == ''; // true
0 == '0'; // true
null == undefined; // true
'1' == 1; // trueUse === (strict equality) always. It compares value and type, no coercion, no surprises. The same goes for !== over !=.
Rule of thumb: if you ever type
==by accident, your linter should slap your wrist. Configure it to.
null vs undefined
Both mean "no value", but they are used differently.
undefinedis what JavaScript gives you. A variable that was never assigned. A function with no return. An object property that does not exist.nullis what you give JavaScript. You set a variable tonullto say "I am intentionally clearing this."
In practice you will use null for API responses, default values, and resets. You will see undefined mostly from missing data.
One quick recap
- Default to
const, fall back tolet, ignorevar. - Seven primitives, everything else is an object.
typeof nullis'object'. Annoying but true.- Always
===, never==. undefinedis the absence,nullis the intent.
That is the entire foundation. Next lesson: functions and the closures that scare people for no reason.
Free tools you can use while you learn
Common questions
Q.Should I use let or const?›
Q.Is == ever safe to use?›
Watching quietly. Tap me if you want a tip.
Try this (0 of 2 done)
- 1
Declare a const named city with value "Bengaluru" and log it.
show answer
const city = "Bengaluru"; console.log(city); - 2
Show that 1 == "1" is true but 1 === "1" is false.
show answer
console.log(1 == "1"); console.log(1 === "1");