ToolPopToolPop
JavaScript · Lesson 2 of 18

Variables, types, and the == trap that has bitten everyone

10 min readUpdated 24 Jun 2026

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

  • const is your default. Use it for everything until the value actually needs to change.
  • let is for values that will be reassigned. Loop counters, accumulators, swapping state.
  • var is legacy. It is function-scoped (not block-scoped) and gets hoisted in confusing ways. Do not use it in new code.
js
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.

  • string for text: 'Bengaluru'
  • number for both integers and decimals: 42, 3.14
  • boolean for true / false
  • undefined when a variable is declared but not set
  • null when you explicitly mean "nothing here"
  • bigint for huge integers, written 9007199254740993n
  • symbol for 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.

js
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.

js
0 == '';        // true
0 == '0';       // true
null == undefined; // true
'1' == 1;       // true

Use === (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.

  • undefined is what JavaScript gives you. A variable that was never assigned. A function with no return. An object property that does not exist.
  • null is what you give JavaScript. You set a variable to null to 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 to let, ignore var.
  • Seven primitives, everything else is an object.
  • typeof null is 'object'. Annoying but true.
  • Always ===, never ==.
  • undefined is the absence, null is 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?
A.Use const by default. Switch to let only when you actually need to reassign the binding. Never use var in new code.
Q.Is == ever safe to use?
A.Only one defensible case: value == null to check for both null and undefined in one shot. Otherwise always use ===. Every other use of == has bitten someone.
Chai0/2 done

Watching quietly. Tap me if you want a tip.

JS Playground
console
// hit run to see output

Try this (0 of 2 done)

  1. 1

    Declare a const named city with value "Bengaluru" and log it.

    show answer
    const city = "Bengaluru";
    console.log(city);
  2. 2

    Show that 1 == "1" is true but 1 === "1" is false.

    show answer
    console.log(1 == "1");
    console.log(1 === "1");