Math built in function javascript
JavaScript provides a built-in Math
object that includes various mathematical functions and constants. Here are some commonly used Math functions in JavaScript:
- Math.round(x):
- Rounds a number to the nearest integer.
- Example:
Math.round(1.2)
returns1
, andMath.round(1.7)
returns2
2.Math.ceil(x):
- Rounds a number upward to the nearest integer (ceiling).
- Example:
Math.ceil(1.2)
returns2
, andMath.ceil(1.7)
returns2
.
3.Math.floor(x):
- Rounds a number downward to the nearest integer (floor).
- Example:
Math.floor(1.2)
returns1
, andMath.floor(1.7)
returns1
.
4.Math.abs(x):
- Returns the absolute value of a number.
- Example:
Math.abs(-5)
returns5
.
5.Math.sqrt(x):
- Returns the square root of a number.
- Example:
Math.sqrt(9)
returns3
.
6.Math.pow(x, y):
- Returns the value of x raised to the power of y.
- Example:
Math.pow(2, 3)
returns8
(2^3).
7.Math.min(x1, x2, …, xn):
- Returns the smallest of the provided numbers.
- Example:
Math.min(5, 3, 7)
returns3
.
8.Math.max(x1, x2, …, xn):
- Returns the largest of the provided numbers.
- Example:
Math.max(5, 3, 7)
returns7
.
9.Math.random():
- Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).
- Example:
Math.random()
returns a value between 0 (inclusive) and 1 (exclusive).
10.Math.PI:
- Represents the mathematical constant Pi (π).
- Example:
Math.PI
returns approximately3.141592653589793
.
11.Math.Expression:
- Represents the mathematical constant e.
- Example:
Math.E
returns approximately2.718281828459045
.
These are just a few examples of the functions provided by the Math
object in JavaScript. They are often used for various mathematical calculations in web development.