Skip to content

Latest commit

 

History

History
108 lines (76 loc) · 2.16 KB

Math.md

File metadata and controls

108 lines (76 loc) · 2.16 KB

Math

Basic arithmetic

  • Addition
  • Subtraction
  • Mutiplication
  • Division

Addition

let number = 100; 
console.log(`${number + 10}`); // 110 

Subtraction

let number = 100; 
console.log(`${number - 50}`); // 50 

Mutiplication

let number = 100; 
console.log(`${number * number}`); // 10000 

Division

let number = 100; 
console.log(`${number / 10}`); // 10 

Increment ++

let currentYear = 2020; 
let nextYear = ++currentYear; 
console.log(`Next year is ${nextYear}`); // Next year is 2021

Decrement --

let countdown = 10; 
--countdown; 
console.log(`Current countdown is ${countdown}`); // Current countdown is 9

Modulo / Remainder operator %

let number = 15; 
let remainder = number % 10; 
console.log(`The remainder is ${remainder}`) // 5

Using the Math object

MDN documentation: Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.

Note: Math works with the Number type. It doesn't work with BigInt.

Pi

const pi = Math.PI; 
console.log(`Pi is ${pi}`); // Pi is 3.141592653589793

sqrt()

const number = 81; 
console.log(`The sqrt of ${number} is ${Math.sqrt(number)}`); // The sqrt of 81 is 9

floor()

MDN: The Math.floor() function returns the largest integer less than or equal to a given number.

const floatMiddleIndex = 3 / 2;
console.log(floatMiddleIndex); // 1.5

const integerMiddleIndex = Math.floor(3 / 2); 
console.log(integerMiddleIndex); // 1

ceil()

MDN: The Math.ceil() function always rounds a number up to the next largest integer.

const floatValue = 3 / 2;
console.log(floatValue); // 1.5

const integerValue = Math.ceil(3 / 2); 
console.log(integerValue); // 2

Resources

  1. MDN - Math Object
  2. MDN - Division