The if statement in JavaScript runs a block of code only when a condition is truthy. You chain else if to test further conditions in order, and a final else runs when none of them matched. This is the basic "javascript if else" decision tool, and nearly every program uses it.
JavaScript evaluates the conditions from top to bottom and runs only the first block whose condition is truthy. The rest are skipped, even if they would also match.
An if statement has a condition in parentheses followed by a block in curly braces. The block runs only when the condition is truthy.
If temperature were 20, the first message would not print, but Done. still would, because it sits outside the block. Always use braces, even for a single statement. Without braces only the next statement belongs to the if, and adding a second line later is a common source of bugs.
else gives you a block that runs when the if condition is falsy. Exactly one of the two blocks runs.
When there are more than two outcomes, add one or more else if branches between if and else. JavaScript checks each condition in order and stops at the first one that is truthy.
The order matters. A score of 95 also satisfies score >= 80, but the first branch already matched, so the later ones never run. If you put the >= 70 test first, almost everything would be graded C. Order conditions from the most specific to the most general.
The condition does not have to be a boolean. JavaScript converts it to true or false using truthiness. These values are falsy: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including "0", "false", empty arrays [] and empty objects {}.
Be careful when 0 or an empty string is a valid value. For example if (count) treats a count of 0 as missing. In that case test explicitly with count !== undefined or use the nullish check count ?? 0.
Use && (and), || (or) and ! (not) to build larger conditions. Both && and || short-circuit, so the right side is only evaluated when needed.
Optional chaining (?.) returns undefined instead of throwing when something along the path is missing, which keeps the condition safe.
Use === and !== to compare values. The loose operators == and != convert types first, which gives surprising results such as 0 == "" being true.
A very common mistake is writing a single =, which assigns instead of comparing:
Use x === 10 instead. Linters such as ESLint flag this with the no-cond-assign rule.
Another pitfall is a long chain that tests the same variable many times. Consider an early return, a lookup object or a switch statement when the structure gets repetitive.
Inside a function, returning early often reads better than nesting else blocks.
The ternary operator condition ? a : b is a compact expression form of if and else. Use it for choosing a value, not for running statements with side effects, and avoid nesting more than one level.
No. JavaScript uses else if, written as two words. It is just an else whose body is another if statement.
Yes, a single statement may follow the condition, but it is error prone. Most style guides and formatters require braces.
With else if, only the first matching branch runs. With separate if statements, every condition is checked and several blocks can run.
Use value == null, which is true for both, or the explicit value === null || value === undefined. The nullish operators ?? and ?. handle many such cases without an if.
if runs a block when its condition is truthy, else handles the remaining case.else if chains tests in order and only the first matching branch runs.false, 0, "", null, undefined, NaN, 0n.===, never confuse = with a comparison, and always use braces.