πŸ’©

programierds

PROGRAMIERDS_OS v1.0.4 // TOPIC: JS_TYPES

> 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

string number boolean undefined null symbol bigint
let x = 42;
let y = x; // copy of the value
y = 99;
// x is still 42

Objects

Complex structures, manipulated by reference

Object Array Function Date RegExp Map Set
let a = { 42 };
let b = a; // reference copy
b.value = 99;
// a.value is also 99

TYPE_01

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 nombre = "Ana";
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
IEEE 754
42
3.14
NaN
Infinity
TYPE_02

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 entero = 42;
const decimal = 3.14;
const grande = 1e308;
const division = 7 / 2; // 3.5
const entera = Math.floor(7 / 2); // 3

TYPE_03

Boolean

The simplest type and the most powerful

1 0 1 0 0 1 1 0 1
0 1 1 0 1 0 0 1 0
1 1 0 0 1 1 0 1 1
0 0 1 1 0 0 1 0 0
1 0 0 1 1 0 1 1 0
0 1 0 0 1 0 1 0 1
1 1 1 0 0 0 1 1 0
0 0 0 1 1 1 0 0 1
1 0 1 1 0 0 0 1 1
0 1 0 1 0 1 1 0 0
1 1 0 1 1 0 0 0 1
0 0 1 0 0 1 1 1 0
1 0 0 0 1 0 1 0 1
0 1 1 1 0 1 0 1 0
true
1 / condition met / on
5 > 3 // true
"hola".length > 0 // true
false
0 / condition not met / off
5 < 3 // false
"".length > 0 // false

FALSY VALUES β€” all of these are false in a boolean

false 0 "" null undefined NaN

TYPES_04 & 05

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"

let nombre;
// nombre === undefined
typeof nombre; // "undefined"

null

Intentional absence of value (you set it)

"the programmer says: there's nothing here, on purpose"

let usuario = null;
// explicitly empty
typeof usuario; // "object" (!)

JS HISTORICAL BUG

typeof null === "object" // true β€” but null is NOT an object
// 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

TYPES_06 & 07

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 a = Symbol("id");
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.

9007199254740993n
typeof 42n; // "bigint"
42n + 1n; // 43n
// Cannot be mixed with Number

OPERATOR

typeof

Your runtime type detector

node β€” typeof inspector
> typeof "hola" "string"
> typeof 42 "number"
> typeof true "boolean"
> typeof undefined "undefined"
> typeof null "object" ⚠ bug!
> typeof Symbol() "symbol"
> typeof 42n "bigint"
> typeof "object"
> typeof [] "object"
> typeof function() "function"

DANGER

Coercion

When JS converts types silently β€” and breaks your code

UNEXPECTED BEHAVIOR

"5"+3= "53" concat!
"5"-3= 2 ok
[] + []= "" 🤔
[] + {}= "[object Object]"
{} + []= 0 😱

THE SOLUTION: == vs ===

 == 

Loose equality β€” converts types before comparing

1 == "1" // true (!!)
0 == false // true (!!)
null == undefined // true
 === 

Strict equality β€” same value AND same type

1 === "1" // false
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