<htmlstart/>

JS Data Types

JavaScript has 7 primitive types and one complex type: object.

Primitive Types

// String — text
const name = "Alice";
const greeting = `Hello, ${name}!`;  // template literal

// Number — integers and decimals (one type)
const age   = 25;
const price = 9.99;
const big   = 1_000_000;  // underscores for readability

// Boolean
const isLoggedIn = true;
const isEmpty    = false;

// Undefined — declared but not assigned
let x;
console.log(x);  // undefined

// Null — intentional absence of value
const data = null;

// BigInt — integers too large for Number
const huge = 9007199254740993n;

// Symbol — unique identifier (advanced)
const id = Symbol("id");

The Object Type

// Object
const user = { name: "Alice", age: 25 };

// Array (is an object)
const colours = ["red", "green", "blue"];

// Function (is an object)
function add(a, b) { return a + b; }

Checking Types

typeof "hello"    // "string"
typeof 42         // "number"
typeof true       // "boolean"
typeof undefined  // "undefined"
typeof null       // "object"  ← famous JS quirk
typeof []         // "object"
typeof {}         // "object"
typeof function(){} // "function"

// Better check for arrays:
Array.isArray([1, 2, 3])  // true

Type Coercion

"5" + 3    // "53"  (number coerced to string)
"5" - 3    // 2     (string coerced to number)
"5" * "2"  // 10
Boolean("")        // false
Boolean("hello")   // true
Boolean(0)         // false
Boolean(1)         // true
Use === (strict equality) not == — strict equality checks both value AND type, avoiding coercion surprises. "5" == 5 is true, but "5" === 5 is false.
🧩

Test Yourself

4 questions
Q1.How many primitive types does JavaScript have?
Q2.What does typeof null return?
Q3.Which operator should you use for equality checks to avoid type coercion?
Q4.What is the result of "5" - 3 in JavaScript?