Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 19, 2021 02:04 am GMT

Your Complete Guide To Math Object in Javascript

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)) // 2console.log(Math.floor(1.999999)) // 1console.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)) // 1console.log(Math.ceil(0) )// 0console.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)) // 0console.log(Math.round(10.5)) // 11console.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)) // 1console.log(Math.trunc(10.5)) // 10console.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)) // -1console.log(Math.sign(0)) // 0console.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)) // 8console.log(Math.pow(1,10)) // 1console.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)) // 4console.log(Math.sqrt(100)) // 10console.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)) // 2console.log(Math.cbrt(27)) // 3console.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)) // 1console.log(Math.log2(8)) // 3console.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)) // 2console.log(Math.min(...[20, 10, 100, 70])) // 10console.log(Math.min(7, "Aya", "Bouchiha", 3)) // NaNconsole.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)) // 8console.log(Math.max(...[20, 10, 100, 70])) // 100console.log(Math.max(7, "Aya", "Bouchiha", 3)) // NaNconsole.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))  // 0console.log(Math.abs(4.5)) // 4.5console.log(Math.abs('')) // 0console.log(Math.abs(null)) // 0console.log(Math.abs([])) // 0console.log(Math.abs('Aya Bouchiha')) // NaNconsole.log(Math.abs([1, 2, 3, 4])) // NaNconsole.log(Math.abs({})) // NaN console.log(Math.abs()) //  NaNconsole.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.5574077246549023console.log(Math.tan(2)) // -2.185039863261519console.log(Math.tan()) // NaNconsole.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.9092974268256817console.log(Math.sin(1)) // 0.8414709848078965console.log(Math.sin()) // NaNconsole.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.4161468365471424console.log(Math.cos(1)) // 0.5403023058681398console.log(Math.cos()) // NaNconsole.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.PIconst CalculateAreaOfACircle = radius => Math.round(Pi* (radius ** 2));console.log(Pi) // 3.141592653589793console.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.Econsole.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

Final Code on github

more details(mdn)...

Have A Nice Day!


Original Link: https://dev.to/ayabouchiha/your-complete-guide-to-math-object-in-javascript-4o5d

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To