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.
Working with constants
Logarithm to base e
Math.E; // 2.718281828459045
Logarithm to base 10
Math.LN10; // 2.302585092994046
Logarithm to base 2
Math.LN2; // 0.6931471805599453
Base 10 logarithm of e
Math.LOG10E; // 0.4342944819032518
Base 2 logarithm of e
Math.LOG2E; // 1.4426950408889634
π₯§
Math.PI; // 3.141592653589793
Square root of 1/2
Math.SQRT1_2; // 0.7071067811865476
Square root of 2
Math.SQRT2; // 1.4142135623730951
Infinity
Infinity; // Infinity
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
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
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
Modulus (%)
Returns the remainder after (integer) division.
42 % 10; // 2
-40 % 10; // -0 π€
Trigonometry
Sine
Math.sin(60); // -0.3048106211022167
Cosine
Math.cos(60); // -0.9524129804151563
Tangent
Math.tan(60); // 0.320040389379563
Incrementing (++)
++
increments its operand by 1.
// postfix: returns the value before incrementing
let a = 4, // 4
b = a++, // 4
c = a; //5
// prefix: returns the value after incrementing
let a = 4, // 4
b = ++a, // 5
c = a; //5
Decrementing (--)
--
decrements its operand by 1.
// postfix: returns the value before decrementing
let a = 4, // 4
b = a--, // 4
c = a; //3
// prefix: returns the value after decrementing
let a = 4, // 4
b = --a, // 3
c = a; //3
Exponentiation (**)
// Math.pow() or ** can be used
let a = 4,
b = 2,
c = Math.pow(a, b), // 16
d = a ** b; // 16
Getting maximum and minimum
Math.max(4.2, 4.7); // 4.7
Math.min(4.2, 4.7); // 4.2
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
Getting roots β
Square Root
Math.sqrt(16); // 4
Cube Root
Math.cbrt(27); // 3
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
Much more complex calculations can be done by combining one or more of these operations.
Top comments (6)
Nice write up. You could add an example with a negative number for
Math.round()
.About the use of
Infinity
, I think itβs useful when you are comparing results. If you do something likeif(1/x > 1/y)
and one ofx
ory
turns out to be0
, then the comparison still works.Thanks! I've added the example and your note on the use of infinity.
On your second decrementing example, did you mean to have only one
-
sign?Oops. My mistake. Fixed. Thanks!
Nice post,how do you add the black background with your coding examples?
Hi! I just set what I want to appear as code in a "code block" by surrounding it with three backticks "`" at the start and at the end. Check this out for examples.