🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
JavaScript

Data Types

JavaScript has numbers, strings (text in quotes), and booleans (true/false). You don't declare the type; JavaScript figures it out.

3 min 10 XP Lesson 3 of 21
Data Types
🌐

Appy Says…

JavaScript has seven primitive types — and getting them confused is the source of more bugs than almost anything else. Know your types and you'll understand why '5' + 5 === '55' but '5' - 5 === 0. Yes, really.

📖

What are Data Types in JavaScript?

Every value in JavaScript has a type that determines what you can do with it. JavaScript has 7 primitive types plus the Object type (which includes arrays and functions).

  • string — text: 'hello', "world", `template`
  • number — integers AND decimals: 42, 3.14, -7
  • booleantrue or false
  • undefined — variable declared but not assigned
  • null — intentional absence of a value
  • bigint — integers too large for number: 9007199254740993n
  • symbol — unique identifier (advanced)
  • Check type: typeof value'string', 'number', etc.
🎮

Think of it like Minecraft item categories

In Minecraft, tools, blocks, food, and weapons are different categories — a sword won't go in the food slot. Data types are JavaScript's categories. You can add numbers, concatenate strings, and toggle booleans — but mixing them without care gives weird results.

⚙️

How It Works

  • 1. JavaScript is dynamically typed — a variable can change type
  • 2. typeof 42'number'; typeof 'hi''string'
  • 3. Type coercion: JS auto-converts types in operations — '5' + 5'55' (number becomes string)
  • 4. Use === (strict equality) to compare type AND value: 5 === '5'false
  • 5. == (loose equality) converts types first: 5 == '5'true (avoid!)
  • 6. Convert explicitly: Number('5'), String(42), Boolean(0)
🌍

Real-World Examples

  • A form returns numbers as strings — use Number(input.value) before maths
  • TikTok's view count is a number; the display text (e.g. '1.2M') is a string
  • Checking 'is the user logged in?' returns a boolean
  • null is returned by APIs when a field has no value (e.g. no profile picture URL)
  • undefined appears when you access a property that doesn't exist on an object
💡

Key Facts

  • typeof null returns 'object' — this is a famous JavaScript bug kept for historical reasons
  • Falsy values: 0, '', null, undefined, NaN, false — all evaluate to false in conditions
  • NaN (Not a Number) is actually of type numbertypeof NaN === 'number'
  • TypeScript was created mainly to add static types to JavaScript — type safety is that important
⚠️

Watch Out!

Never use == (loose equality) — it performs type coercion in surprising ways. Always use === (strict equality). 0 == false is true and '' == false is true — these will silently break your logic.

📌

Remember

Always use === not ==. When mixing types, convert explicitly with Number(), String(), or Boolean().

What You Learned

  • 7 primitive types: string, number, boolean, null, undefined, bigint, symbol
  • Use === for strict comparison; use typeof to inspect types
  • Unlocks: avoiding type-coercion bugs, safe form handling, confident API data parsing

Key Facts

  • typeof null returns 'object' — this is a famous JavaScript bug kept for historical reasons
  • Falsy values: 0, '', null, undefined, NaN, false — all evaluate to false in conditions
  • NaN (Not a Number) is actually of type numbertypeof NaN === 'number'
  • TypeScript was created mainly to add static types to JavaScript — type safety is that important

Real-World Examples

• A form returns numbers as strings — use <code>Number(input.value)</code> before maths • TikTok's view count is a <code>number</code>; the display text (e.g. '1.2M') is a <code>string</code> • Checking 'is the user logged in?' returns a <code>boolean</code> • <code>null</code> is returned by APIs when a field has no value (e.g. no profile picture URL) • <code>undefined</code> appears when you access a property that doesn't exist on an object

Remember

Always use === not ==. When mixing types, convert explicitly with Number(), String(), or Boolean().

Quick Quiz

1 / 2

What type is true?