Solice UI
Back to the curriculum

Chapter 4 · JavaScript & the DOM

Conditionals and loops

Interactive pages rarely do exactly the same thing every time. Conditionals choose a path from current facts, while loops apply the same operation to a collection of values.

Beginner · Lesson 2 of 6 in this chapter

A conditional expresses a decision

An if statement runs code only when its condition is true. An else branch handles the other case. Keep the condition close to the user-visible decision so the behavior stays easy to read.

A loop handles a collection

A loop visits each item without repeating the same statement manually. Array methods such as map, filter, and find often express common collection work more clearly than a general loop.

Showing search results

  1. If the query is empty, show the complete list.
  2. Filter the lessons when the query has text.
  3. If the filtered list is empty, show one clear recovery message.
  • Conditionals connect facts to behavior.
  • Loops remove repeated code for collections.
  • Choose the clearest construct, not the most clever one.