JS

JavaScript Fundamentals

25 lessons

Progress0%
1. Introduction to JavaScript
1What is JavaScript?2Setting Up Your Environment
2. Variables and Data Types
1Declaring Variables2Data Types3Type Conversion
3. Operators
Arithmetic OperatorsComparison OperatorsLogical Operators
4. Control Flow
Conditional StatementsLoops
5. Functions
Function Basics
6. Arrays & Iteration
Array MethodsSpread, Rest & Destructuring
7. Objects & JSON
Working with ObjectsJSON & Optional Chaining
8. OOP & Classes
Class BasicsInheritance & Private Fields
9. Modules & Modern JS
ES ModulesModern JavaScript Features
10. Async JavaScript
PromisesAsync/Await
11. Error Handling
Error Types & try/catchCustom Errors & Debugging
12. Iterators & Advanced
Iterators & GeneratorsMap, Set & WeakRefs
All Tutorials
JavaScriptControl Flow
Lesson 9 of 25 min
Chapter 4 · Lesson 1

Conditional Statements

Conditional statements let your code make decisions based on conditions.

if Statement: Executes a block of code if the condition is true.

if...else Statement: Executes one block if true, another if false.

if...else if...else: Chain multiple conditions together.

Ternary Operator: Shorthand for simple if/else: condition ? trueValue : falseValue

switch Statement: Compare a value against multiple cases. Good for multiple specific values.

Best Practices:

  • Keep conditions simple and readable
  • Use early returns to reduce nesting
  • Consider switch for 3+ conditions on the same value
  • Use the ternary operator only for simple expressions

Code Examples

If/Else Statementsjavascript
// Simple if
let age = 20;

if (age >= 18) {
  console.log("You are an adult");
}

// if...else
let temperature = 15;

if (temperature > 25) {
  console.log("It's hot!");
} else {
  console.log("It's not hot");
}

// if...else if...else
let score = 85;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else if (score >= 70) {
  console.log("Grade: C");
} else {
  console.log("Grade: F");
}

// Ternary operator
let status = age >= 18 ? "adult" : "minor";
console.log("Status:", status);

// Nested ternary (use sparingly!)
let category = age < 13 ? "child" : age < 20 ? "teen" : "adult";
console.log("Category:", category);

The ternary operator is great for simple conditions, but use if/else for complex logic to maintain readability.

Switch Statementjavascript
let day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start of the work week");
    break;
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
    console.log("Midweek");
    break;
  case "Friday":
    console.log("TGIF!");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend!");
    break;
  default:
    console.log("Invalid day");
}

// Switch with expressions (less common but useful)
let grade = "B";
let message;

switch (true) {
  case grade === "A":
    message = "Excellent!";
    break;
  case grade === "B" || grade === "C":
    message = "Good job!";
    break;
  default:
    message = "Keep trying!";
}

console.log(message);

Don't forget the break statement! Without it, execution 'falls through' to the next case.

Quick Quiz

1. What happens if you forget 'break' in a switch statement?

2. What is the ternary operator syntax?

Was this lesson helpful?

PreviousNext