</>
Skip to content
JavaScript lessons (14/64)

JavaScript — Data Types

Eight types, two families

Everything you store in a variable has a type:

Primitives (simple, copied by value):

TypeExamples
string"hello", 'A'
number42, 3.14, -7, Infinity, NaN
booleantrue, false
undefineddeclared but never assigned
nulldeliberately "nothing"
bigint9007199254740993n — huge integers
symbolrare, advanced identifiers

Objects (containers, shared by reference): plain objects, arrays, functions, dates…

Inspecting with typeof

typeof "hi"        // "string"
typeof 42          // "number"
typeof true        // "boolean"
typeof undefined   // "undefined"
typeof null        // "object"  ← historic bug, officially kept forever
typeof {}          // "object"
typeof []          // "object"  ← arrays too!
typeof function(){}// "function"

Two classics hiding there:

  • typeof null === "object" — a 1995 bug frozen into the language
  • Arrays report "object" — check with Array.isArray(x) instead

Strings — text in quotes

const single = 'single';
const double = "double";
const template = `backticks`;    // most powerful

const name = "Ada";
console.log(`Hi ${name}, ${2 + 2}!`);   // Hi Ada, 4!

Backtick template literals interpolate any expression inside ${} — modern code defaults to them.

Numbers — one type for all

No separate integer/float like other languages:

10, 3.14, -0.5      // all the same "number"
10 / 3              // 3.3333333333333335
0.1 + 0.2           // 0.30000000000000004  (binary float math)
(0.1 + 0.2).toFixed(2)   // "0.30"

Special members:

NaN            // "Not a Number" — failed math result
isNaN("abc")   // true
Infinity       // division by zero, overflow

Booleans — decisions' currency

let loggedIn = true;
if (loggedIn) { /* … */ }

Truthy / falsy — JS's coercion habit

Any value squeezed into a boolean context lands on one side:

falsy:  false, 0, "", null, undefined, NaN
truthy: everything else — "0", [], {}, "false"
if ("hello") console.log("runs!");   // non-empty string = truthy

This powers idioms like if (userInput) {…} — but also surprises: "0" is truthy!

undefined vs null — the eternal interview question

let x;           // undefined — "never given a value"
let y = null;    // null      — "intentionally empty"

Both falsy, both mean absence — one accidental, one deliberate.

Objects and arrays (a first look)

const person = { name: "Ada", age: 36 };   // keyed container
const colors = ["red", "green", "blue"];   // ordered list

person.name         // "Ada"     dot access
colors[0]           // "red"     index access (from 0!)
colors.length       // 3

Each gets full lessons soon — here just recognize them as objects, the reference-based family:

const a = [1, 2];
const b = a;         // copies the REFERENCE, not the array
b.push(3);
console.log(a);      // [1, 2, 3] — both names see the same array!

Primitives copy values; objects share references. That single sentence explains half of beginner JS bugs.

Dynamic typing

Variables aren't locked to a type:

let thing = 42;        // number
thing = "forty-two";   // now a string — legal!
thing = [thing];       // now an array

Freedom with a price: typos and wrong-type bugs surface at runtime, not while writing. This is precisely why TypeScript exists.

Mini Practice

  1. typeof eight different values including null and an array
  2. Prove 0.1 + 0.2 !== 0.3; fix display with .toFixed(2)
  3. Test five values for truthiness with Boolean(value)
  4. Demonstrate reference sharing: push to b, print a
  5. Reassign one variable through three types; log typeof after each hop

Next: functions → (already written)

Related Topics

Frequently Asked Questions about Data Types

What is Data Types in JavaScript?

Data Types is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Data Types?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.

Why is Data Types important in JavaScript?

Data Types is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.