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

    Operators and strings · Lesson 9 of 45

    Numbers and Math

    Rounding, random numbers and formatting.

    7 minutes3 games

    The Math object holds ready-made number tools, and Number has helpers for converting and checking.

    JavaScript
    console.log(Math.round(4.5))   // 5
    console.log(Math.floor(4.9))   // 4: down
    console.log(Math.ceil(4.1))    // 5: up
    console.log(Math.max(3, 9, 2)) // 9
    console.log(Math.abs(-7))      // 7
    console.log((19.9876).toFixed(2)) // "19.99"
    console.log(Number.isInteger(5.0)) // true

    Random numbers

    Math.random() returns a random number from 0 up to (not including) 1. For a whole number from 1 to 6 (a die):

    JavaScript
    const dice = Math.floor(Math.random() * 6) + 1
    console.log("You rolled", dice)

    Run it a few times; you'll get different numbers.

    Math.random() → 0 … 0.999

    * 6 → 0 … 5.99

    Math.floor → 0 … 5

    + 1 → 1 … 6

    Important

    toFixed returns a string, not a number. Convert with Number() if you'll calculate with it.

    Interview question

    How do you get a random whole number between min and max inclusive?

    What's the output?

    console.log(Math.floor(7.8))
    console.log(Math.ceil(7.2))

    What will this code print?

    Quick question

    What does (3.456).toFixed(1) return?

    Exercise

    Print the largest number using Math.max (42).

    Expected output: 42

    JavaScript
    const scores = [12, 42, 7, 30]
    

    Learn more from

    • javascript.info: Numbersjavascript.info
    • MDN: Mathdeveloper.mozilla.org
    • MDN: Math.random()developer.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousString methodsNextConditions: if and else
    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.