Your Complete Guide To Math Object in Javascript
Aya Bouchiha
Posted on July 19, 2021
Hi everybody, I'm Aya Bouchiha, and this is your complete guide to Math object in Javascript!
Math Object
Firstly, we need to know that Math is a built-in object, It works with Number type and not with BigInt. In addition, It is not a constructor. All its properties and its methods are static.
Math.floor()
Math.floor(number) : rounds a number down and returns an integer value.
/* rounds a number down */
console.log(Math.floor(2.4)) // 2
console.log(Math.floor(1.999999)) // 1
console.log(Math.floor(10.5)) // 10
Math.ceil()
Math.ceil(number) : rounds a number up to the next largest integer.
/* rounds a number up to the next largest integer */
console.log(Math.ceil(0.000000000000000001)) // 1
console.log(Math.ceil(0) )// 0
console.log(Math.ceil(10.5)) // 11
Math.random()
Math.random() : returns a random number where 0 <= Math.random() < 1
/* get a random number n where 0 <= n < 1 */
console.log(Math.random()) // 0.3594237846698176
// Returns a random integer from 0 to 50:
console.log(Math.floor(Math.random() * 50)) // 43
Math.round()
Math.round(number) : rounds to the nearest integer
/* rounds to the nearest integer */
console.log(Math.round(0.2)) // 0
console.log(Math.round(10.5)) // 11
console.log(Math.round(1.9) )// 2
Math.trunc()
Math.trunc(number) : returns the integer part of a number by removing any fractional digits.
/* get the integer part of a number */
console.log(Math.trunc(1.000000001)) // 1
console.log(Math.trunc(10.5)) // 10
console.log(Math.trunc(4.999999)) // 4
Math.sign()
Math.sign(number) :indicate the sign of a numeber. If the num is:
- negative : returns -1
- 0 : returns 0
- positive : returns 1
/* get the sign of a number */
console.log(Math.sign(-4.5)) // -1
console.log(Math.sign(0)) // 0
console.log(Math.sign(10)) // 1
Math.pow()
Math.pow(base, exponent) : returns baseexponent.
/* get the value of a num1 to the power of a num2 */
console.log(Math.pow(2,3)) // 8
console.log(Math.pow(1,10)) // 1
console.log(Math.pow(10,3)) // 1000
Math.sqrt()
Math.sqrt(num) : returns the square root of a number.
/* get the square root of a number. */
console.log(Math.sqrt(16)) // 4
console.log(Math.sqrt(100)) // 10
console.log(Math.sqrt(25)) // 5
Math.cbrt()
Math.cbrt(num) : returns the cubic root of a number.
/* get the cubic root of a number. */
console.log(Math.cbrt(8)) // 2
console.log(Math.cbrt(27)) // 3
console.log(Math.cbrt(64)) // 4
Math.log2()
Math.log2(num) : returns the base 2 logarithm of a number,
/* get the base 2 logarithm of a number */
console.log(Math.log2(2)) // 1
console.log(Math.log2(8)) // 3
console.log(Math.log2(16)) // 4
Math.min()
Math.min(n1, n2, n3, ..) : returns The smallest number of the given numbers. If one of the giving arguments is not a number It returns NaN, and Infinity if no arguments are passed.
/* get the smallest of the given numbers. */
console.log(Math.min(2, 4, 6, 8)) // 2
console.log(Math.min(...[20, 10, 100, 70])) // 10
console.log(Math.min(7, "Aya", "Bouchiha", 3)) // NaN
console.log(Math.min()) // Infinity
Math.max()
Math.max(n1, n2, n3, ..) : returns The biggest number of the given numbers. If one of the giving arguments is not a number it returns NaN, and -Infinity if no arguments are passed.
/* get the biggest num of the given numbers. */
console.log(Math.max(2, 4, 6, 8)) // 8
console.log(Math.max(...[20, 10, 100, 70])) // 100
console.log(Math.max(7, "Aya", "Bouchiha", 3)) // NaN
console.log(Math.max()) // -Infinity
Math.abs()
Math.abs(num) : returns the absolute value of the giving number. This method can accept also numeric strings like '-1'
.
- It returns NaN if the giving argument is not a number or is not a numeric-string, or It is undefined or empty .
- It returns 0 if the argument is
null
,[]
,''
or""
/* get the absolute value of the given number */
console.log(Math.abs(-2)) // 2
console.log(Math.abs(0)) // 0
console.log(Math.abs(4.5)) // 4.5
console.log(Math.abs('')) // 0
console.log(Math.abs(null)) // 0
console.log(Math.abs([])) // 0
console.log(Math.abs('Aya Bouchiha')) // NaN
console.log(Math.abs([1, 2, 3, 4])) // NaN
console.log(Math.abs({})) // NaN
console.log(Math.abs()) // NaN
console.log(Math.abs(undefined)) // NaN
Math.tan()
Math.tan(angleInRadians) : returns the tangent of a giving angle(radians).
/* get the tangent an angle(radians) */
console.log(Math.tan(1)) // 1.5574077246549023
console.log(Math.tan(2)) // -2.185039863261519
console.log(Math.tan()) // NaN
console.log(Math.tan('')) // 0
Math.sin()
Math.sin(angleInRadians) : returns the sine of a giving angle
(radians), in addition, it is between -1 & 1.
/* get the sine of an angle(radians) */
console.log(Math.sin(2)) // 0.9092974268256817
console.log(Math.sin(1)) // 0.8414709848078965
console.log(Math.sin()) // NaN
console.log(Math.sin('')) // 0
Math.cos()
Math.cos(angleInRadians) : returns the cosine of a giving angle
(radians), in addition, it is between -1 & 1.
/* get the cosine of an angle(radians) */
console.log(Math.cos(2)) // -0.4161468365471424
console.log(Math.cos(1)) // 0.5403023058681398
console.log(Math.cos()) // NaN
console.log(Math.cos('')) // 1
Math.exp()
Math.exp(number) : returns ex
Math.Pi
Math.Pi : is a static property of Math that returns the value of PI (approximately 3.14)
/* PI */
const Pi = Math.PI
const CalculateAreaOfACircle = radius => Math.round(Pi* (radius ** 2));
console.log(Pi) // 3.141592653589793
console.log(CalculateAreaOfACircle(4)) // 50
Math.E
Math.E : is a static property of Math that returns the value of Euler's number (approximately 2.718)
/* Euler's numbe */
const e = Math.E
console.log(e) // 2.718281828459045
Math.LN2
Math.E : is a static property of Math that returns the natural logarithm of 2 (approximately 0.693)
/* natural logarithm of 2 */
console.log(Math.LN2) // 0.6931471805599453
Final Code
Have A Nice Day!
- email: developer.aya.b@gmail.com
- telegram: Aya Bouchiha
Posted on July 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 26, 2024