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
- String → Text
"hello",'Shoaib',`Shaikh` - Number → Any numeric value
3,-99,3.14 - Boolean → True or False
true,false - Undefined → Variable declared but not assigned
let x;
x // Output → undefined
- Null → Intentional empty value
let x = null;
- Symbol → Unique identifier (rarely used)
- 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
- Object →
{ name: "Shoaib", age: 22 } - Array →
[10, 20, 30] - Function →
function 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",
NaNis actually of typenumber. - This is because operations like
0 / 0orparseInt("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 nullis"object"— this is a bug.undefinedmeans the variable was never assigned.nullmeans you intentionally set it to "nothing".'5' + 1is"51"but'5' - 1is4.
Practice Zone
- Predict the output:
console.log(null + 1);
console.log("5" + 3);
console.log("5" - 3);
console.log(true + false);
- Check types:
console.log(typeof []);
console.log(typeof null);
console.log(typeof 123n);
- Truthy or Falsy?
console.log(Boolean(0)); // falsy
console.log(Boolean("0")); // truthy
console.log(Boolean([])); // truthy
console.log(Boolean(undefined)); // falsy
- Write a function
isEmpty(value)that checks if a given value isnull,undefined, or"". - Compare with loose vs strict:
console.log(5 == "5"); // ?
console.log(5 === "5"); // ?
