Slater New
« back to Blog

Javascript 101: Javascript Math

Jared Malan
Director Of Technology
 @
Edgar Allan

Let's build off of the random dynamic loading script highlighted at the beginning of this email. To create that experience, we needed to generate a random number. Luckily this is fairly trivial because of Javascript's built-in math capabilities. Some of the Math functions are super useful (e.g. random numbers, rounding) for even the most basic Javascript developers. Others will only be used by the most seasoned.

Basic Arithmetic

You can use the standard arithmetic operators for basic calculations:

let a = 5;
let b = 2;

console.log(a + b); // Addition: 7
console.log(a - b); // Subtraction: 3
console.log(a * b); // Multiplication: 10
console.log(a / b); // Division: 2.5
console.log(a % b); // Modulus (remainder): 1
console.log(a ** b); // Exponentiation: 25

Math Object

The Math object provides several properties and methods for mathematical constants and functions.

Constants

  • Math.PI: Returns the value of π (3.14159...)
  • Math.E: Returns the value of Euler's number (e)

console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045

Rounding Functions

  • Math.round(): Rounds to the nearest integer
  • Math.ceil(): Rounds up to the next largest integer
  • Math.floor(): Rounds down to the next smallest integer
  • Math.trunc(): Truncates the decimal part

let num = 5.7;

console.log(Math.round(num)); // 6
console.log(Math.ceil(num)); // 6
console.log(Math.floor(num)); // 5
console.log(Math.trunc(num)); // 5

Random Numbers

  • Math.random(): Returns a random number between 0 (inclusive) and 1 (exclusive)

console.log(Math.random()); // e.g., 0.4971539834831

To get a random number between a specified range:

functiongetRandomInt(min, max) {
returnMath.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(1, 10)); // Random integer between 1 and 10

Absolute Value

  • Math.abs(): Returns the absolute value of a number

console.log(Math.abs(-5)); // 5

Trigonometric Functions

  • Math.sin(), Math.cos(), Math.tan(): Sine, cosine, and tangent functions (arguments in radians)
  • Math.asin(), Math.acos(), Math.atan(): Inverse trigonometric functions

console.log(Math.sin(Math.PI / 2)); // 1
console.log(Math.cos(0)); // 1
console.log(Math.tan(Math.PI / 4)); // 1

Other Useful Functions

  • Math.sqrt(): Square root
  • Math.pow(): Power
  • Math.exp(): Exponential function (e^x)
  • Math.log(): Natural logarithm (base e)
  • Math.max(), Math.min(): Maximum and minimum values from a list of numbers

console.log(Math.sqrt(25)); // 5
console.log(Math.pow(2, 3)); // 8
console.log(Math.exp(1)); // 2.718281828459045 (approximation of e)
console.log(Math.log(Math.E)); // 1
console.log(Math.max(1, 5, 3)); // 5
console.log(Math.min(1, 5, 3)); // 1

This should give you a good starting point for working with math in JavaScript! If you have any specific questions or need more details, feel free to ask.

Try it out for yourself

Sign up for a free trial and see if Slater is for you.

Share this post

We are building a first-class development experience that bridges together code and no-code.