Skdev Online Academy logo
JavaScript

Chapter 2: Data Types + Type System

Learn about JavaScript data types, primitive vs reference types, typeof operator, coercion, truthy/falsy values, and type system concepts.

What are Data Types?

  • In JavaScript, every value has a type.
  • These types define what kind of data is being stored - number, text, boolean, object, etc.

There are two categories:

  • Primitive types - stored directly
  • Reference / Non-Primitive types - stored as memory references

Primitive Data Types

  1. String → Text
    "hello", 'Shoaib', `Shaikh`
  2. Number → Any numeric value
    3, -99, 3.14
  3. Boolean → True or False
    true, false
  4. Undefined → Variable declared but not assigned
let x;
x // Output → undefined
  1. Null → Intentional empty value
let x = null;
  1. Symbol → Unique identifier (rarely used)
  2. BigInt → Very large integers

123456789012345678901234567n

These are copied directly:

let a = 2;
let b = a;
b = b + 1;
b; // output → 3
a; // output → 2

Reference / Non-Primitive Data Types

  1. Object{ name: "Shoaib", age: 22 }
  2. Array[10, 20, 30]
  3. Functionfunction studentName() { }

These are not copied directly, but by reference:

let a = [10, 20, 30];
let b = a;
b.pop();
b; // As expected the output will be → [10, 20]
a; // But the output of a is also → [10, 20]

typeof Operator

  • Used to check the data type of a value:
typeof "Shoaib" // "string"
typeof 99 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" ← known bug
typeof [] // "object"
typeof {} // "object"
typeof function(){} // "function"
Note:typeof null === "object" is a bug, but has existed since the early days of JS.

Type Coercion (Auto-Conversion)

  • JavaScript auto-converts types in some operations:
"5" + 1 // "51" → number converted to string
"5" - 1 // 4 → string converted to number
true + 1 // 2
null + 1 // 1
undefined + 1 // NaN

Loose vs Strict Equality

  • == compares value with type conversion
  • === compares value + type (no conversion)
5 == "5" // true
5 === "5" // false
  • Always prefer === for accurate comparisons.

NaN – Not a Number

typeof NaN // "number"
  • Even though it means "Not a Number", NaN is actually of type number.
  • This is because operations like 0 / 0 or parseInt("abc") still produce a numeric result — just an invalid one.

Truthy and Falsy Values

Falsy values:

false, 0, "", null, undefined, NaN

Everything else is truthy, including:

Truthy values:

"0", "false", [], {}, function(){}

// Example:
if ("0") {
  console.log("Runs"); // "0" is a non-empty string = truthy
}

Mindset

  • JavaScript will often auto-convert types behind the scenes.
  • Always stay aware of what data type you're working with.

Common Confusions

  • typeof null is "object" — this is a bug.
  • undefined means the variable was never assigned.
  • null means you intentionally set it to "nothing".
  • '5' + 1 is "51" but '5' - 1 is 4.

Practice Zone

  1. Predict the output:
console.log(null + 1);
console.log("5" + 3);
console.log("5" - 3);
console.log(true + false);
  1. Check types:
console.log(typeof []);
console.log(typeof null);
console.log(typeof 123n);
  1. Truthy or Falsy?
console.log(Boolean(0)); // falsy
console.log(Boolean("0")); // truthy
console.log(Boolean([])); // truthy
console.log(Boolean(undefined)); // falsy
  1. Write a function isEmpty(value) that checks if a given value is null, undefined, or "".
  2. Compare with loose vs strict:
console.log(5 == "5"); // ?
console.log(5 === "5"); // ?