JavaScript Lesson 3: Conditions and Loops

JavaScript Lesson 3: Conditions and Loops featured illustration

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 1 min read

πŸ“˜ JavaScript Beginner Course Β· Lesson 3 of 8

Advertisement
In this article
  1. Comparisons
  2. Loops
  3. Practice
  4. Continue learning
const marks = { Aarav: 78, Diya: 91, Kabir: 45, Rohan: 33 };
for (const [name, m] of Object.entries(marks)) {
  const grade = m >= 90 ? "A+" : m >= 75 ? "A" : m >= 40 ? "Pass" : "Fail";
  console.log(name, m, grade);
}

Comparisons

Use === and !== (strict: same value and type). == converts types and surprises people (“0” == 0 is true). Combine with && (and), || (or), ! (not).

Loops

  • for (let i = 1; i <= 3; i++): counting loops.
  • for (const x of array): each item of an array.
  • Object.entries(obj): key-value pairs of an object.
  • while (condition): repeat until false.
πŸ’‘ The ternary a ? b : c is great for short choices; switch to if/else blocks when it gets longer than one readable line.

Practice

Run the script and add a grade for marks above 60.

Advertisement

πŸ“Ž Practice files for this article

⬇ Download all 2 files (ZIP Β· 616 B)

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong