Home >Web Front-end >JS Tutorial >10 useful JavaScript Math object methods (recommended)

10 useful JavaScript Math object methods (recommended)

青灯夜游
青灯夜游forward
2020-10-30 17:26:022391browse

10 useful JavaScript Math object methods (recommended)

Recommended tutorial: "JavaScript Video Tutorial"

math in JavaScript allows us to perform some mathematics on operate. It has properties and methods for mathematical constants and functions. In today's article, we will introduce some useful methods of the Math object.

1. Math.min()

Math.min() is a function in the JS math library, used to return the minimum value of all passed values Give the method.

Math.min(0, 150, 30, 20, -8, -200) // -200

2. Math.max()

Math.max() method can return the larger value of the two specified numbers.

Math.max(0, 150, 30, 20, -8, -200) // 150

3. Math.round()

Math.round() The function returns a number rounded to the nearest integer.

Math.round(4.7) // 5
Math.round(4.4) // 4

4. Math.sqrt()

Math.sqrt() The function returns the square root of a number, that is:

10 useful JavaScript Math object methods (recommended)

Math.sqrt(64) // 8
Math.sqrt(25) // 5

5. Math.pow()

Math.pow() The function returns the exponent power of the base (base), that is:

Math.pow(8, 2) // 64

6. Math.floor()

Math.floor() Returns the largest integer less than or equal to a given number.

Math.floor(4.7) // 4
Math.floor(8.6) // 8

7. Math.random()

Math.random() The function returns a floating point, pseudo-random number in the range from 0 to less than 1, that is, from 0 (including 0) upwards, but excluding 1 (excluding 1) and then you can zoom to the desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be selected or reset by the user.

Math.random()  // 0.15497907645259867

8. Math.cos()

Math.cos() The function returns the cosine of a value.

Math.cos(0, Math.PI / 180)  // 1

9. Math.sin()

Math.sin() The function returns the sine of a value.

Math.sin(90 * Math.PI / 180) // 1

Math.ceil() The function returns the smallest integer greater than or equal to a given number.

Math.ceil(4.4) // 5

Summary

The JS Math object is very powerful and useful when performing some numerical operations. In addition to the above 10 methods, the Math object has many other methods. This is left to everyone. Go check out the document yourself. That’s it for today’s sharing. Thank you all for watching. See you in the next issue.

Original address: https://medium.com/javascript-in-plain-english/top-10-javascript-methods-of-the-math-object-ac69951386a5

Author: Mehdi Aoussiad

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of 10 useful JavaScript Math object methods (recommended). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete