Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    Control flow · Lesson 12 of 45

    Loops: for and while

    Repeat something without writing it again and again.

    9 minutes4 games

    A for loop has 3 parts in its brackets, separated by ;:

    • start: let i = 1 (runs once).
    • condition: i <= 3 (keep going while true).
    • step: i++ (after every round).
    Step by step1 / 11
    1. ▶let total = 0
    2. 2for (let i = 1; i <= 3; i++) {
    3. 3 total += i
    4. 4 console.log("i =", i, "total =", total)
    5. 5}

    Variables

    total0

    Output

    …

    Press “Play” and watch i and total change.

    while and do…while

    while repeats while its condition is true; it suits cases where you don't know the number of rounds in advance. do…while is the same but always runs at least once before checking.

    JavaScript
    let coins = 3
    while (coins > 0) {
      console.log("Spend a coin, left:", coins - 1)
      coins--
    }
    
    let tries = 0
    do {
      tries++
    } while (tries < 1)
    console.log("tries:", tries)

    Remember to change something inside while so the condition eventually becomes false. Forget it and the loop runs forever and freezes the page.

    break and continue

    JavaScript
    for (let i = 1; i <= 10; i++) {
      if (i % 2 === 0) continue  // skip even numbers
      if (i > 7) break           // stop completely
      console.log(i)
    }

    Important

    continue skips to the next round; break stops the whole loop.

    Interview question

    When do you use for versus while?

    Complete the code

    Complete the loop to print 0 to 4.

    for (let i = ; i < ; i++) {
      console.log(i)
    }

    Spot the bug

    This loop never ends. Where's the problem?

    What's the output?

    for (let i = 0; i < 6; i++) {
      if (i === 2) continue
      if (i === 4) break
      console.log(i)
    }

    What will this code print?

    Exercise

    Print the sum of 1 to 10 (the result is 55).

    Expected output: 55

    JavaScript
    let sum = 0
    

    Learn more from

    • javascript.info: Loops: while and forjavascript.info
    • MDN: Loops and iterationdeveloper.mozilla.org

    Played 0 of 4 games

    Sign in to save your progress and earn XP.

    Previousswitch and the ternary operatorNextChallenge: FizzBuzz
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.