🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  1. Unit 2: Intro to Code
  2. Variables & Data
  3. Numbers (Values)

The Math Object

Javascript (and some other languages) have built-in mathematics commands to simplify your life. In JS this is called the Math object.

PreviousConverting & RoundingNextRandom Numbers

Last updated 6 years ago

The various abilities of the Math object can be found in various . Here is a sampling of what you might use.

Code

Description

Math.PI

Returns 3.141592653589793

Math.pow(base, exponent)

Returns the base raised to the exponent

Math.sqrt(number)

Returns the square root of the given number

Math.round(number)

Rounds the given number to the nearest integer

Math.ceil(number)

Returns the number rounded up to the nearest integer

Ex. Math.ceil(5.1) returns 6

Math.floor(number)

Returns the number rounded down to the nearest integer

Ex. Math.floor(5.8) returns 5

Math.random()

Returns a decimal number between 0 and 1. Use it in conjunction with Math.floor() to return an integer.

locations online
pseudorandom