> Data Types
in JavaScript
Every value has an identity. Get to know them.
↓ scroll to continue
Primitives vs Objects
The data universe has TWO fundamental categories
Primitives
Simple values, copied by value
let y = x; // copy of the value
y = 99;
// x is still 42
Objects
Complex structures, manipulated by reference
let b = a; // reference copy
b.value = 99;
// a.value is also 99
String
Text strings β sequences of UTF-16 code units
- βΊ Declared with single or double quotes
- βΊ Immutable β any operation returns a NEW string
- βΊ Template literals use backticks
const saludo = `Hola, ${nombre}!`;
const largo = nombre.length; // 3
nombre[0]; // "A"
// nombre[0] = "B" β not allowed
each character occupies a consecutive memory cell
Number
One type for all numbers β integers and floats
KEY
JS uses number for both integers and floating-point numbers.
SPECIAL CASES
NaN β result of invalid math operation
Infinity β division by zero or overflow
const decimal = 3.14;
const grande = 1e308;
const division = 7 / 2; // 3.5
const entera = Math.floor(7 / 2); // 3
Boolean
The simplest type and the most powerful
"hola".length > 0 // true
"".length > 0 // false
FALSY VALUES β all of these are false in a boolean
undefined vs null
The most misunderstood distinction in JS. Pay attention.
undefined
Declared variable but no value assigned
"the system says: I don't know what's here"
// nombre === undefined
typeof nombre; // "undefined"
null
Intentional absence of value (you set it)
"the programmer says: there's nothing here, on purpose"
// explicitly empty
typeof usuario; // "object" (!)
JS HISTORICAL BUG
// This bug exists since 1995 and can't be fixed (would break the web)
null == undefined // true (loose)
null === undefined // false (strict) β they are different
Symbol & BigInt
The two most modern primitives in JS
Symbol
Unique and unrepeatable value. Each Symbol() is different from all others, even with the same description.
const b = Symbol("id");
a === b; // false (!!!)
typeof a; // "symbol"
// Use: private property keys
BigInt
For integers LARGER than Number.MAX_SAFE_INTEGER (2^53 - 1). Written with n at the end.
42n + 1n; // 43n
// Cannot be mixed with Number
typeof
Your runtime type detector
Coercion
When JS converts types silently β and breaks your code
UNEXPECTED BEHAVIOR
THE SOLUTION: == vs ===
Loose equality β converts types before comparing
0 == false // true (!!)
null == undefined // true
Strict equality β same value AND same type
0 === false // false
null === undefined // false
Rule: ALWAYS use ===. Use == only if you know exactly why.
FINAL REPORT
All types at a glance
| Type | Category | typeof | Example | Note |
|---|---|---|---|---|
| string | primitive | "string" | "hola" | Immutable |
| number | primitive | "number" | 42, 3.14, NaN | IEEE 754 |
| boolean | primitive | "boolean" | true / false | Flow control |
| undefined | primitive | "undefined" | let x; | No value assigned |
| null | primitive | "object" ⚠ | null | Intentional emptiness |
| symbol | primitive | "symbol" | Symbol("id") | Unique and unrepeatable |
| bigint | primitive | "bigint" | 9007...n | Large integers |
| object | object | "object" | {}, [], null | By reference |
| function | object | "function" | function() | Special object |
END OF SESSION // DATA PROCESSED CORRECTLY
← BACK TO PRESENTATIONS