Lesson 10
Conditionals
Conditionals allow your program to make decisions and execute different code based on different conditions.
- if Statement
- if...else Statement
- else if Ladder
- Nested if
- Ternary Operator
if
Executes a block of code if the condition is true.
if (condition) { // code}if...else
Executes code if condition is true, otherwise executes another block.
if (condition) { // code A} else { // code B}else if Ladder
Checks multiple conditions in sequence.
if (cond1) { // code A} else if (cond2) { // code B} else { // code C}Nested if
An if statement inside another if statement.
if (cond1) { if (cond2) { // code }}Ternary Operator
A shorthand way to write if...else.
let result = condition ? val1 : val2;JavaScript
Output
Your output will appear here...
if (condition) { ... }if (condition) { ... }else { ... }if (a) { ... } else if (b) { ... }else { ... }condition ? trueValue : falseValue
Conditions in JavaScript evaluate to either true or false. Most values are “truthy” except 0, "", null, undefined, NaN, and false.
Conditionals help your program make decisions. Use if for a single condition, if...else for two possibilities, else if for multiple choices, and the ternary operator for simple conditions.
