Skip to content

Operadores

Operadores Fundamental

Section titled “Operadores ”

Operadores aritméticos Básico

Section titled “Operadores aritméticos ”
let suma = 5 + 3; // 8
let resta = 5 - 3; // 2
let multiplicacion = 5 * 3; // 15
let division = 15 / 3; // 5
let modulo = 5 % 2; // 1 (resto)
let exponente = 2 ** 3; // 8 (2³)
let numero = 5;
numero++; // Post-incremento
++numero; // Pre-incremento
numero--; // Post-decremento
--numero; // Pre-decremento
5 == "5" // true (compara valor)
5 != "5" // false
5 === "5" // false (compara valor y tipo)
5 !== "5" // true
5 > 3 // true
5 >= 5 // true
3 < 5 // true
3 <= 3 // true
true && true // true
true && false // false
false && true // false
false && false // false
true || true // true
true || false // true
false || true // true
false || false // false
!true // false
!false // true
null ?? "valor" // "valor"
undefined ?? "valor" // "valor"
0 ?? "valor" // 0
"" ?? "valor" // ""
let x = 5;
x += 3; // x = x + 3
x -= 3; // x = x - 3
x *= 3; // x = x * 3
x /= 3; // x = x / 3
x %= 3; // x = x % 3
x **= 3; // x = x ** 3
let edad = 20;
let mensaje = edad >= 18 ? "Mayor de edad" : "Menor de edad";
let a = 5; // 101 en binario
let b = 3; // 011 en binario
a & b // AND: 001 (1)
a | b // OR: 111 (7)
a ^ b // XOR: 110 (6)
~a // NOT: -6
a << 1 // Left shift: 1010 (10)
a >> 1 // Right shift: 010 (2)
let resultado = 2 + 3 * 4; // 14 (no 20)
let conParentesis = (2 + 3) * 4; // 20
const usuario = {
direccion: {
calle: "Principal"
}
};
const calle = usuario?.direccion?.calle; // "Principal"
const numero = usuario?.direccion?.numero; // undefined
  1. Usar comparación estricta (=== y !==)
// ✅ Bueno
if (valor === null) {
// código
}
  1. Evitar operadores de incremento/decremento en expresiones complejas
// ❌ Malo
let x = y++ + ++z;
// ✅ Bueno
y++;
++z;
let x = y + z;
  1. Usar paréntesis para clarificar precedencia
// ✅ Bueno
let resultado = (a + b) * c;
  1. Utilizar el operador nullish coalescing para valores por defecto
// ✅ Bueno
const valor = entrada ?? valorPorDefecto;
🐝