๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
โšก JavaScript

Conditions (if/else)

๐Ÿ“š What are conditions in JavaScript? They let your script **branch**: run one path when something is true, another when it is false. Use `if`, `else if`, and `else` with conditions in parentheses.

5 min 10 XP Lesson 4 of 21
Conditions (if/else)
๐ŸŒ

Appy Saysโ€ฆ

JavaScript has all the same conditionals as Python, but there's one powerful addition: the ternary operator. It lets you write a simple if/else in a single line โ€” used constantly in modern React apps.

๐Ÿ“–

Conditions in JavaScript

JavaScript uses if / else if / else โ€” the same logic as Python but with curly braces {} instead of indentation.

  • โ€ขif (condition) { ... } โ€” runs when condition is truthy
  • โ€ขelse if (condition) { ... } โ€” chains additional checks
  • โ€ขelse { ... } โ€” the fallback
  • โ€ขStrict equality: use === (not ==) to compare both value AND type
  • โ€ขTernary: const label = score >= 50 ? "Pass" : "Fail";
๐ŸŽฎ

=== vs == โ€” strict bouncer vs lazy bouncer

The double equals == is a lazy bouncer โ€” it accepts "5" == 5 as true because it converts types. The triple equals === is strict โ€” it checks type AND value, so "5" === 5 is false. Always use strict equality to avoid sneaky bugs.

๐ŸŒ

Real-World Examples

  • โ€ขButton text: const btnText = isLoading ? "Loading..." : "Submit";
  • โ€ขTheme toggle: const theme = isDark ? "dark" : "light";
  • โ€ขAuth guard: if (!isLoggedIn) { redirect('/login'); }
  • โ€ขGrade display: if (score >= 90) grade = 'A*'; else if (score >= 80) grade = 'A'; ...
๐Ÿ’ก

Key Facts

  • โ€ขTruthy/falsy: 0, "", null, undefined, NaN are all falsy in JS โ€” treated as False in conditions
  • โ€ขThe ! operator negates: if (!isLoggedIn) means 'if not logged in'
  • โ€ขswitch (value) { case x: ... } โ€” useful for many possible values
  • โ€ขLogical AND: &&; logical OR: ||; nullish coalescing: ??
๐Ÿ“Œ

Remember

Always use === instead of == in JavaScript. The triple equals checks both value and type, preventing a whole class of subtle bugs that beginners (and even pros) fall into with ==.

โœ…

What You Learned

  • โ€ขif () {} else if () {} else {} with curly braces (not indentation)
  • โ€ขAlways use === for comparison (strict, checks type too)
  • โ€ขTernary: condition ? valueIfTrue : valueIfFalse
  • โ€ขFalsy values: 0, "", null, undefined, NaN
  • โ€ขLogical operators: && (and), || (or), ! (not)

Key Facts

  • โ†’Truthy/falsy: 0, "", null, undefined, NaN are all falsy in JS โ€” treated as False in conditions
  • โ†’The ! operator negates: if (!isLoggedIn) means 'if not logged in'
  • โ†’switch (value) { case x: ... } โ€” useful for many possible values
  • โ†’Logical AND: &&; logical OR: ||; nullish coalescing: ??

Real-World Examples

โ€ข Button text: <code>const btnText = isLoading ? "Loading..." : "Submit";</code> โ€ข Theme toggle: <code>const theme = isDark ? "dark" : "light";</code> โ€ข Auth guard: <code>if (!isLoggedIn) { redirect('/login'); }</code> โ€ข Grade display: <code>if (score &gt;= 90) grade = 'A*'; else if (score &gt;= 80) grade = 'A'; ...</code>

Remember

Always use === instead of == in JavaScript. The triple equals checks both value and type, preventing a whole class of subtle bugs that beginners (and even pros) fall into with ==.

Quick Quiz

1 / 2

What do we put inside if ()?