JavaScript Lesson 3: Conditions and Loops

📎 This article includes 2 downloadable practice files ↓

⏱ 1 min read

📘 JavaScript Beginner Course · Lesson 3 of 8

In this article
  1. Comparisons
  2. Loops
  3. Practice

“`javascript
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.

📎 Practice files for this article

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