Beginner
+20 XP

👋 Start learning JavaScript right now — for free!

📖

7 JavaScript Types

Understand what types of data exist in JS and how to tell them apart

🗂️

Types — different shelves in a wardrobe

Imagine a wardrobe with labeled shelves: one for numbers, one for text, one for on/off switches. JavaScript also categorizes information — types. Knowing the type, the computer knows what to do with it: add numbers, concatenate text, check booleans.

7 JavaScript Types

JavaScript has 7 types — 6 primitive and 1 complex:

TypeExampleWhen used
string"Hello", 'JS'Text, names, messages
number42, 3.14, -7Numbers, prices, coordinates
booleantrue, falseYes/No, on/off, conditions
undefinedundefinedVariable declared but has no value
nullnullIntentionally empty value
symbolSymbol('id')Unique keys (advanced level)
bigint9007199254740993nVery large numbers
object{}, [], nullComplex structures (next lesson!)

The first 6 are primitives (simple values). object is the complex type.

The typeof operator

typeof returns the type name as a string:

js
typeof "Hello"    // "string"
typeof 42          // "number"
typeof 3.14        // "number"  — decimals are also number!
typeof true        // "boolean"
typeof undefined   // "undefined"
typeof null        // "object"  ← GOTCHA! (explained next lesson)
typeof {}          // "object"
typeof []          // "object"  ← arrays are also object!
typeof function(){} // "function"

Important: typeof returns a string in quotes — i.e. "number", "string", not just number.

Strings (string) — in detail

js
let name = "Maria";       // double quotes
let city = 'London';       // single (same thing)
let greeting = `Hello, ${name}!`; // template literal — inserts variable

console.log(greeting);     // Hello, Maria!

// String length
console.log(name.length);  // 5

// Concatenation (joining)
let full = "John" + " " + "Smith"; // "John Smith"

Template literals (backtick `) — the most convenient way to insert a variable into text.

Numbers — NaN and Infinity

js
console.log(10 / 0);         // Infinity (not an error!)
console.log(-10 / 0);        // -Infinity
console.log("hello" * 2);    // NaN — Not a Number

// NaN — is a number type, but 'not a number' in value
console.log(typeof NaN);     // "number" ← yes, NaN is number!

// Converting string to number
console.log(parseInt("42px"));   // 42 — takes number from beginning
console.log(parseFloat("3.14")); // 3.14
console.log(Number("42"));       // 42
console.log(Number("hello"));    // NaN

Checking NaN: isNaN(value) or Number.isNaN(value). You can't compare value === NaN — always false!

Boolean — truthy and falsy values

In JavaScript every value is automatically either 'truthy' or 'falsy' when checked in a condition:

Falsy — exactly 7 values:

js
false
0
-0
0n        // BigInt zero
""        // empty string
null
undefined
NaN

Truthy — EVERYTHING else, including:

js
true
1
-1
"0"       // string '0' — truthy!
"false"   // string 'false' — truthy!
[]        // empty array — truthy!
{}        // empty object — truthy!

This matters for if conditions: if (value) — checks if value is truthy.

javascript
💬

Gotcha with empty array — very common mistake. [] is truthy but 'empty'. Check .length!

null vs undefined

Both mean 'nothing here', but with different intent:

js
let a;                    // undefined — value not set
console.log(a);           // undefined

let b = null;             // null — intentionally EMPTY
console.log(b);           // null

console.log(typeof a);    // "undefined"
console.log(typeof b);    // "object" ← surprise!

Rule:

  • undefined — the variable exists but no value was assigned (JS sets it)
  • null — you explicitly say 'nothing here'

For example: let user = null; — the user hasn't loaded yet.

Remember: typeof returns a STRING. So check like: typeof x === "number" (with quotes). And typeof null === "object" — this is a JS bug from 1995, never fixed to avoid breaking old code.

Comments

Log In or Start to leave a comment.