JavaScript Math
Provides most of the functions exposed by the Math library (opens in a new tab) in JS.
-
Math.floor():
- Returns the largest integer less than or equal to a given number.
console.log(Math.floor(4.7)); // Output: 4
-
Math.abs():
- Returns the absolute value of a number.
console.log(Math.abs(-4.7)); // Output: 4.7
-
Math.ceil():
- Returns the smallest integer greater than or equal to a given number.
console.log(Math.ceil(4.4)); // Output: 5
-
Math.round():
- Rounds a number to the nearest integer.
console.log(Math.round(4.4)); // Output: 4
-
Math.trunc():
- Truncates the decimal part of a number, returning the integer part.
console.log(Math.trunc(4.7)); // Output: 4
-
Math.log():
- Returns the natural logarithm (base e) of a number.
console.log(Math.log(10)); // Output: 2.302585092994046
-
Math.pow():
- Returns the base to the exponent power.
console.log(Math.pow(2, 3)); // Output: 8
-
Math.acos():
- Returns the arccosine (in radians) of a number.
console.log(Math.acos(0.5)); // Output: 1.0471975511965979
-
Math.asin():
- Returns the arcsine (in radians) of a number.
console.log(Math.asin(0.5)); // Output: 0.5235987755982989
-
Math.atan():
- Returns the arctangent (in radians) of a number.
console.log(Math.atan(1)); // Output: 0.7853981633974483
-
Math.atan2():
- Returns the arctangent of the quotient of its arguments.
console.log(Math.atan2(1, 1)); // Output: 0.7853981633974483
-
Math.cos():
- Returns the cosine of a number.
console.log(Math.cos(0)); // Output: 1
-
Math.exp():
- Returns e^x, where x is the argument.
console.log(Math.exp(1)); // Output: 2.718281828459045
-
Math.max():
- Returns the largest of zero or more numbers.
console.log(Math.max(10, 20)); // Output: 20
-
Math.min():
- Returns the smallest of zero or more numbers.
console.log(Math.min(10, 20)); // Output: 10
-
Math.sin():
- Returns the sine of a number.
console.log(Math.sin(0)); // Output: 0
-
Math.sqrt():
- Returns the square root of a number.
console.log(Math.sqrt(9)); // Output: 3
-
Math.tan():
- Returns the tangent of a number.
console.log(Math.tan(0)); // Output: 0
-
Math.random():
- Returns a random number between 0 and 1.
console.log(Math.random()); // Output: a random number between 0 and 1
Each of these functions can be called using the Math
object followed by the function name and the necessary arguments enclosed in parentheses.