P3.2: JS Control Flow & Loops

Control flow allows programs to make decisions and execute code repeatedly.


1. Conditionals (if/else)

Decisions are evaluated using Boolean logic:

const score = 85;
if (score >= 90) {
  console.log("Grade A");
} else if (score >= 80) {
  console.log("Grade B");
} else {
  console.log("Try again!");
}
Douglas Crockford

Douglas CrockfordView Profile 🔗

Real-World Developer Case Study
25 Years Exp
RoleAuthor of JavaScript: The Good Parts
Salary₹95 Lakhs/yr equivalent
CompanyPayPal Fellow (ex)
Core Tech Stack
JavaScript SpecsJSON StandardLinting Tools

Always use strict equality operators (=== and !==) instead of loose comparisons (==). Loose comparisons execute silent type conversions, leading to bugs.


2. Iteration Loops (for & while)

Execute blocks multiple times:

for (let i = 0; i < 5; i++) {
  console.log("Iteration: " + i);
}

🎓 Practice Assignment Tasks

  • Write an `if/else` block that checks if an age variable is greater than or equal to 18.
  • Write a `for` loop that logs even numbers from 0 to 10.
Stuck? Hints available
// Write your JavaScript logic here
// Click 'Run Code' to see console outputs below
🔒 Code requirements not met yet. Complete the tasks above.
🔒 Complete the Practice Assignment above to unlock completion.
Advertisement
NS
Ask Nextsem
AI Mode
NS
Home
Course
Playground