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 11 of 45

    switch and the ternary operator

    Shorter alternatives for many choices or just two.

    7 minutes3 games

    The ternary is a one-line condition: condition ? ifTrue : ifFalse. Perfect for choosing between two values.

    JavaScript
    const stock = 0
    const label = stock > 0 ? "In stock" : "Sold out"
    console.log(label)
    
    const items = 1
    console.log(`${items} ${items === 1 ? "item" : "items"}`)

    switch

    switch compares one value against several options (using ===). Each case needs a break, or execution falls into the next case. default runs if nothing matched.

    JavaScript
    const day = "fri"
    switch (day) {
      case "fri":
      case "sat":
        console.log("Weekend")
        break
      case "sun":
        console.log("Back to work")
        break
      default:
        console.log("Weekday")
    }

    fri and sat share code: a case without a break falls through on purpose.

    Important

    Don't nest ternaries: three levels deep becomes unreadable. Use if or switch instead.

    Interview question

    What happens if you forget a break in a switch?

    What's the output?

    const n = 2
    switch (n) {
      case 1:
        console.log("one")
      case 2:
        console.log("two")
      case 3:
        console.log("three")
    }

    What will this code print?

    Complete the code

    Write the ternary: Pass for 50 or more, otherwise Fail.

    const result = score >= 50  "Pass"  "Fail"

    Exercise

    Use a ternary to print even or odd for n.

    Expected output: odd

    JavaScript
    const n = 7
    

    Learn more from

    • javascript.info: The "switch" statementjavascript.info
    • MDN: Conditional (ternary) operatordeveloper.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousConditions: if and elseNextLoops: for and while
    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.