💩

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, inmutables, copiados por valor

string number boolean undefined null symbol bigint
let x = 42;
let y = x; // copia del valor
y = 99;
// x sigue siendo 42

Objetos

Estructuras complejas, copiados por referencia

Object Array Function Date RegExp Map / Set
let a = { score: 42 };
let b = a; // referencia!
b.score = 99;
// a.score también es 99

TIPO_01

String

Cadenas de texto — secuencia de caracteres Unicode

  • Delimitado por " ", ' ' o ` `
  • Los backticks permiten template literals
  • Inmutable: no podés cambiar un char individualmente
const nombre = "Ana";
const saludo = `Hola, ${nombre}!`;
typeof nombre; // "string"
nombre.length; // 3
nombre[0]; // "A"

cada letra es un character en memoria


Number
IEEE 754
42
3.14
NaN
Infinity
TIPO_02

Number

Un solo tipo para todos los números

CLAVE

JS NO tiene int ni float. Solo number (IEEE 754 de 64 bits).

CASOS ESPECIALES

NaN — "Not a Number" (resultado de operación inválida)

Infinity — división por cero

typeof 42; // "number"
typeof 3.14; // "number"
typeof NaN; // "number" (!)
1 / 0; // Infinity
Number("abc"); // NaN

TIPO_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

TIPOS_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;
// explícitamente vacío
typeof usuario; // "object" (!)

EL BUG HISTORICO DE JS

typeof null === "object" // true — pero null NO es un objeto
// Este bug existe desde 1995 y no se puede arreglar (rompería internet)
null == undefined // true (loose)
null === undefined // false (strict) — son distintos

TIPOS_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"
// Uso: property keys privadas

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
// No se puede mezclar con Number

OPERADOR

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