Javascript Arithmetic Cheat Sheet

Kelvin Wangonya - Mar 5 '19 - - Dev Community

Given that one of the main reason computers were invented was to solve mathematical problems quickly, it is no wonder that all the modern programming languages are so rich in arithmetic-oriented methods. The earliest computers were basically just calculators. (Yes, I'm looking at you Abacus). If you dabble in Javascript (and a little math every now and then), I do hope you find this useful. The very obvious operations like simple addition (+) and subtraction (-) have been omitted. So have more advanced operations.

meme

Working with constants

Logarithm to base e

Math.E; // 2.718281828459045
Enter fullscreen mode Exit fullscreen mode

Logarithm to base 10

Math.LN10; // 2.302585092994046
Enter fullscreen mode Exit fullscreen mode

Logarithm to base 2

Math.LN2; // 0.6931471805599453
Enter fullscreen mode Exit fullscreen mode

Base 10 logarithm of e

Math.LOG10E; // 0.4342944819032518
Enter fullscreen mode Exit fullscreen mode

Base 2 logarithm of e

Math.LOG2E; // 1.4426950408889634
Enter fullscreen mode Exit fullscreen mode

🥧

Math.PI; // 3.141592653589793
Enter fullscreen mode Exit fullscreen mode

Square root of 1/2

Math.SQRT1_2; // 0.7071067811865476
Enter fullscreen mode Exit fullscreen mode

Square root of 2

Math.SQRT2; // 1.4142135623730951
Enter fullscreen mode Exit fullscreen mode

Infinity

Infinity; // Infinity
Enter fullscreen mode Exit fullscreen mode

UPDATE: As clarified by @oscherler in the comments,

About the use of Infinity, I think it’s useful when you are comparing results. If you do something like if(1/x > 1/y) and one of x or y turns out to be 0, then the comparison still works.

Rounding

Math.round returns the value of a number rounded to the nearest integer.

Math.round(4.2); // 4
Math.round(4.7); // 5
Math.round(4.5); // 5. Half-way values are always rounded up
Math.round(-4.5); // -4
Enter fullscreen mode Exit fullscreen mode

Speaking of rounding up, Math.ceil():

Math.ceil(4.2); // 5
Math.ceil(4.7); // 5
Math.ceil(-4.7); // -4. Ceiling a negative number will round towards zero
Enter fullscreen mode Exit fullscreen mode

Math.floor() rounds down:

Math.floor(4.2); // 4
Math.floor(4.7); // 4
Math.floor(-4.7); // -5. Flooring a negative number will round away from zero
Enter fullscreen mode Exit fullscreen mode

Modulus (%)

Returns the remainder after (integer) division.

42 % 10; // 2
-40 % 10; // -0 🤔
Enter fullscreen mode Exit fullscreen mode

Trigonometry

Sine

Math.sin(60); // -0.3048106211022167
Enter fullscreen mode Exit fullscreen mode

Cosine

Math.cos(60); // -0.9524129804151563
Enter fullscreen mode Exit fullscreen mode

Tangent

Math.tan(60); // 0.320040389379563
Enter fullscreen mode Exit fullscreen mode

Incrementing (++)

++ increments its operand by 1.

// postfix: returns the value before incrementing
let a = 4, // 4
  b = a++, // 4
  c = a; //5
Enter fullscreen mode Exit fullscreen mode
// prefix: returns the value after incrementing
let a = 4, // 4
  b = ++a, // 5
  c = a; //5
Enter fullscreen mode Exit fullscreen mode

Decrementing (--)

-- decrements its operand by 1.

// postfix: returns the value before decrementing
let a = 4, // 4
  b = a--, // 4
  c = a; //3
Enter fullscreen mode Exit fullscreen mode
// prefix: returns the value after decrementing
let a = 4, // 4
  b = --a, // 3
  c = a; //3
Enter fullscreen mode Exit fullscreen mode

Exponentiation (**)

// Math.pow() or ** can be used
let a = 4,
  b = 2,
  c = Math.pow(a, b), // 16
  d = a ** b; // 16
Enter fullscreen mode Exit fullscreen mode

Getting maximum and minimum

Math.max(4.2, 4.7); // 4.7
Math.min(4.2, 4.7); // 4.2
Enter fullscreen mode Exit fullscreen mode

Getting maximum and minimum from an array:

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9],
  max = Math.max(...arr), // 9
  min = Math.min(...arr); // 1
Enter fullscreen mode Exit fullscreen mode

Getting roots √

Square Root

Math.sqrt(16); // 4
Enter fullscreen mode Exit fullscreen mode

Cube Root

Math.cbrt(27); // 3
Enter fullscreen mode Exit fullscreen mode

To find the nth-root, use the Math.pow() function and pass in a fractional exponent.

// This finds the 6th root of 64
Math.pow(64, 1 / 6); // 4
Enter fullscreen mode Exit fullscreen mode

Much more complex calculations can be done by combining one or more of these operations.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player