- Addition
- Subtraction
- Mutiplication
- Division
let number = 100;
console.log(`${number + 10}`); // 110
let number = 100;
console.log(`${number - 50}`); // 50
let number = 100;
console.log(`${number * number}`); // 10000
let number = 100;
console.log(`${number / 10}`); // 10
let currentYear = 2020;
let nextYear = ++currentYear;
console.log(`Next year is ${nextYear}`); // Next year is 2021
let countdown = 10;
--countdown;
console.log(`Current countdown is ${countdown}`); // Current countdown is 9
let number = 15;
let remainder = number % 10;
console.log(`The remainder is ${remainder}`) // 5
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.
const pi = Math.PI;
console.log(`Pi is ${pi}`); // Pi is 3.141592653589793
const number = 81;
console.log(`The sqrt of ${number} is ${Math.sqrt(number)}`); // The sqrt of 81 is 9
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
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