💩

programierds

PROGRAMIERDS_OS v1.0.4 // TOPIC: JS_TYPES

> Tipos de Datos

en JavaScript

Cada valor tiene una identidad. Conocelas.

↓ scroll para continuar


Primitivos vs Objetos

El universo de los datos tiene DOS categorías fundamentales

Primitivos

Valores simples, copiados por valor

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

Objetos

Estructuras complejas, manipuladas por referencia

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

Cadenas de texto — secuencias de unidades de código UTF-16

  • Se declaran con comillas simples o dobles
  • Inmutable — cualquier operación devuelve un NUEVO string
  • Los template literals usan backticks
const nombre = "Ana";
const saludo = `Hola, ${nombre}!`;
const largo = nombre.length; // 3
nombre[0]; // "A"
// nombre[0] = "B" ❌ not allowed

cada letra ocupa una celda consecutivas en memoria


number
IEEE 754
42
3.14
NaN
Infinity
TYPE_02

Number

Un solo tipo para todos los números — enteros y decimales

CLAVE

JS usa number para enteros y números de punto flotante.

CASOS ESPECIALES

NaN — resultado de operación matemática inválida

Infinity — división por cero o desbordamiento

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

El tipo más simple y el más poderoso

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 / condición cumplida / encendido
5 > 3 // true
"hola".length > 0 // true
false
0 / condición no cumplida / apagado
5 < 3 // false
"".length > 0 // false

FALSY VALUES — todo esto es falso en un booleano

false 0 "" null undefined NaN

TYPES_04 & 05

undefined vs null

La distinción más malinterpretada de JS. Prestá atención.

?

undefined

Variable declarada pero sin valor asignado

"el sistema dice: no sé qué hay acá"

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

null

Ausencia intencional de valor (lo pusiste vos)

"el programador dice: acá no hay nada, a propósito"

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

EL BUG HISTORICO DE JS

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

Los dos primitivos más modernos de JS

📍

Symbol

Valor único e irrepetible. Cada Symbol() es diferente a todos los demás, incluso con la misma descripción.

const a = Symbol("id");
const b = Symbol("id");
a === b; // false (!!!)
typeof a; // "symbol"
// Use: private property keys

BigInt

Para enteros MÁS GRANDES que Number.MAX_SAFE_INTEGER (2^53 - 1). Se escribe con n al final.

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

OPERATOR

typeof

Tu detector de tipos en runtime

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"

PELIGRO

Coercion

Cuando JS convierte tipos en silencio — y te rompe el código

COMPORTAMIENTO INESPERADO

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

LA SOLUCION: == vs ===

 == 

Igualdad débil — convierte tipos antes de comparar

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

Igualdad estricta — mismo valor Y mismo tipo

1 === "1" // false
0 === false // false
null === undefined // false

Regla: SIEMPRE usá ===. Usá == solo si sabés exactamente por qué.


REPORTE FINAL

Todos los tipos, de un vistazo

Tipo Categoría typeof Ejemplo Nota
string primitivo "string" "hola" Inmutable
number primitivo "number" 42, 3.14, NaN IEEE 754
boolean primitivo "boolean" true / false Control de flujo
undefined primitivo "undefined" let x; Sin valor asignado
null primitivo "object" null Vacío intencional
symbol primitivo "symbol" Symbol("id") Único e irrepetible
bigint primitivo "bigint" 9007...n Enteros grandes
object objeto "object" {}, [], null Por referencia
function objeto "function" function() Objeto especial

FIN DE SESIÓN // DATOS PROCESADOS CORRECTAMENTE

← VOLVER A PRESENTACIONES