Lesson 17 min read

Variables & Data Types

Give your data a name tag so JavaScript knows what to call it

What Are Variables?

Imagine you have a bunch of labeled jars in your kitchen. One says "sugar," another says "flour." You can peek inside any jar to see what's in it, swap the contents, or empty it out. Variables are exactly like those jars — they hold a piece of data and give it a name you can use later.

In JavaScript, you create a variable with one of three keywords: let, const, or var.

  • let — your go-to choice. You can change what's inside later.
  • const — a locked jar. Once you put something in, you can't swap it out.
  • var — the old-school way. It still works, but it has some quirky behavior that can trip you up. Prefer let and const instead.

Creating Variables

let playerName = "Alex";
let score = 0;
const maxLives = 3;
score = 42; // ✅ let allows reassignment
// maxLives = 5; // ❌ TypeError — const can't be reassigned
console.log(playerName); // "Alex"
console.log(score); // 42
console.log(maxLives); // 3
Output
Alex
42
3

JavaScript's Data Types

Every value in JavaScript has a type — it's how JS knows whether you're dealing with a number, some text, or something else entirely. There are 7 primitive types (simple, single values) and 1 object type (complex, can hold many values).

Here are the primitives you'll use every day:

  • Number — any numeric value: 42, 3.14, -7. JavaScript doesn't separate integers and decimals — they're all just "numbers."
  • String — text wrapped in quotes: "hello", 'world', or backticks `hi`.
  • Boolean — only two possible values: true or false. Think of it as a light switch.
  • Undefined — a variable that exists but hasn't been given a value yet. Like an empty jar with a label on it.
  • Null — you intentionally say "this jar is empty." It's your way of saying "nothing here on purpose."
  • BigInt — for really, really huge numbers beyond what Number can handle.
  • Symbol — a unique identifier. You probably won't need these until later.

typeof — Checking What Type Something Is

console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" 😱 (a famous JS bug!)
console.log(typeof NaN); // "number" (yep, "Not a Number" is a number)
console.log(typeof {}); // "object"
console.log(typeof []); // "object" (arrays are objects too!)
Output
number
string
boolean
undefined
object
number
object
object

The Weirdness of NaN and null

NaN stands for "Not a Number," but typeof NaN returns "number". Wild, right? It shows up when a math operation goes wrong — like trying to multiply a word by a number.

null is supposed to mean "no value," yet typeof null returns "object". This is actually a bug from 1995 that was never fixed because too much code already depended on it. Welcome to JavaScript!

To check for NaN, use Number.isNaN(). To check for null, just use === null.

NaN Gotchas

let result = "apple" * 3;
console.log(result); // NaN
console.log(result === NaN); // false (NaN is not equal to anything, even itself!)
console.log(Number.isNaN(result)); // true ✅ the correct way
let empty = null;
console.log(empty === null); // true
console.log(empty === undefined); // false (null and undefined are different!)
Output
NaN
false
true
true
false
Note: Think of let vs const like a whiteboard vs a plaque. A whiteboard (let) you can erase and rewrite anytime. A plaque (const) is engraved — once it's set, it stays. When in doubt, start with const and switch to let only when you need to change the value later.

Quick check

What will typeof null return in JavaScript?
Operators & Expressions