Conditional Statements in JavaScript

HoistingSwitch

Conditional statements let your code make decisions and run different blocks based on whether a condition is true or false. This is essential for creating programs that respond differently to different situations.

The most common way is the if statement, often combined with else if and else.

The Basic if Statement

If the condition in parentheses is true, the code inside the curly braces runs.

Adding else

else runs when the condition is false.

else if for Multiple Conditions

Chain multiple checks with else if.

Comparison and Logical Operators in Conditions

Use ==/===, >/<, && (and), || (or), ! (not).

Truthy and Falsy Values

JavaScript treats certain values as false in conditions (falsy): false, 0, "", null, undefined, NaN. Everything else is truthy.

The Ternary Operator (Short if/else)

A compact way: condition ? valueIfTrue : valueIfFalse

Nesting Conditionals

You can put if statements inside other if statements for complex logic.

Quick Quiz

Hemos revisado y comprobado los materiales, pero aún pueden existir errores. El contenido se ofrece únicamente con fines educativos, así que úsalo bajo tu propia responsabilidad y verifica con otras fuentes si es necesario.

✨ Pregunta a Lara — tu compañera de estudio con IA

Desbloquea soporte de aprendizaje personalizado. Lara puede explicar lecciones, resumir temas y responder tus preguntas — disponible desde el plan Go y superiores.


Lara te ayuda a aprender más rápido — exclusivo para los miembros ReadyTools Go, Plus y Max.

Sigue Tu Progreso 🚀

Aprende más fácilmente siguiendo tu progreso completamente gratis.