Skip to content

Tipos de datos

Tipos de datos Fundamental

Section titled “Tipos de datos ”
let nombre = "Juan";
let apellido = 'Pérez';
let mensaje = `Hola ${nombre}`; // Template literal
let entero = 42;
let decimal = 3.14;
let infinito = Infinity;
let noEsNumero = NaN;
let verdadero = true;
let falso = false;
let valorNulo = null;
let valorIndefinido = undefined;
let simbolo = Symbol('descripción');
let simboloUnico = Symbol.for('clave');
let numeroGrande = 9007199254740991n;
let otroBigInt = BigInt("9007199254740991");
let persona = {
nombre: "María",
edad: 25,
saludar() {
return `Hola, soy ${this.nombre}`;
}
};
let numeros = [1, 2, 3, 4, 5];
let mixto = ["texto", 42, true, null];
typeof "Hola" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (¡esto es un bug histórico!)
typeof {} // "object"
typeof [] // "object"
typeof Symbol() // "symbol"
typeof 42n // "bigint"
[] instanceof Array // true
new Date() instanceof Date // true
/regex/ instanceof RegExp // true
String(123) // "123"
(123).toString() // "123"
123 + "" // "123"
Number("123") // 123
parseInt("123") // 123
parseFloat("3.14") // 3.14
+"123" // 123
Boolean(1) // true
Boolean("") // false
Boolean([]) // true
!!"" // false
  • false
  • 0
  • ""
  • null
  • undefined
  • NaN
if ("") {
console.log("Esto no se ejecuta");
}
  • Todo lo demás es truthy
if ("texto") {
console.log("Esto sí se ejecuta");
}
"5" + 3 // "53" (string)
"5" - 3 // 2 (number)
"5" * "3" // 15 (number)
[] + {} // "[object Object]"
  1. Usar comparación estricta
// ❌ Malo
5 == "5" // true
// ✅ Bueno
5 === "5" // false
  1. Verificar tipos explícitamente
// ✅ Bueno
if (typeof variable === "string") {
// hacer algo
}
  1. Manejar valores nulos/indefinidos
// ✅ Bueno
const valor = obtenerValor() ?? valorPorDefecto;
🐝