Let's briefly talk about JavaScript's Math object method

WBOY
Release: 2022-08-04 14:43:11
Original
1831 people have browsed it

This article brings you relevant knowledge aboutjavascript, which mainly introduces related issues about the Math object method. The Math object is a built-in object of JavaScript, providing a series of mathematical constants and mathematical Method, this object is not a constructor, so it cannot generate an instance. All properties and methods must be called on the Math object. Let’s take a look at it together. I hope it will be helpful to everyone.

Let's briefly talk about JavaScript's Math object method

[Related recommendations:javascript video tutorial,web front-end

The Math object is a built-in JavaScript Object that provides a set of mathematical constants and mathematical methods.

This object is not a constructor, so an instance cannot be generated. All properties and methods must be called on the Math object.

new Math() // TypeError: object is not a function
Copy after login

The above code indicates that Math cannot be used as a constructor.

Properties

The Math object provides the following read-only mathematical constants.

  • E: constant e.

  • LN2: The natural logarithm of 2.

  • LN10: The natural logarithm of 10.

  • LOG2E: The base 2 logarithm of e.

  • LOG10E: The base 10 logarithm of e.

  • PI: Constant Pi.

  • SQRT1_2: The square root of 0.5.

  • SQRT2: The square root of 2.

The values of these constants are as follows.

Math.E // 2.718281828459045 Math.LN2 // 0.6931471805599453 Math.LN10 // 2.302585092994046 Math.LOG2E // 1.4426950408889634 Math.LOG10E // 0.4342944819032518 Math.PI // 3.141592653589793 Math.SQRT1_2 // 0.7071067811865476 Math.SQRT2 // 1.4142135623730951
Copy after login

Methods

The Math object provides the following mathematical methods.

round method

The round method is used for rounding.

Math.round(0.1) // 0 Math.round(0.5) // 1
Copy after login

Its operation results for negative values are slightly different from positive values, mainly reflected in the processing of .5.

Math.round(-1.1) // -1 Math.round(-1.5) // -1
Copy after login

abs method, max method, min method

abs method returns the absolute value of the parameter value.

Math.abs(1) // 1 Math.abs(-1) // 1
Copy after login

The max method returns the largest parameter, and the min method returns the smallest parameter.

Math.max(2, -1, 5) // 5 Math.min(2, -1, 5) // -1
Copy after login

floor method, ceil method

The floor method returns the largest integer less than the parameter value.

Math.floor(3.2) // 3 Math.floor(-3.2) // -4
Copy after login

The ceil method returns the smallest integer greater than the parameter value.

Math.ceil(3.2) // 4 Math.ceil(-3.2) // -3
Copy after login

pow method, sqrt method

power method returns the exponential value with the first parameter as the base and the second parameter as the power .

Math.pow(2, 2) // 4 Math.pow(2, 3) // 8
Copy after login

sqrt method returns the square root of the parameter value. If the argument is a negative value, NaN is returned.

Math.sqrt(4) // 2 Math.sqrt(-4) // NaN
Copy after login

log method, exp method

log method returns the natural logarithm value with e as the base.

Math.log(Math.E) // 1 Math.log(10) // 2.302585092994046
Copy after login

To find the logarithm of base 10, you can divide it by Math.LN10; to find the logarithm of base 2, you can divide it by Math.LN2.

Math.log(100)/Math.LN10 // 2 Math.log(8)/Math.LN2 // 3
Copy after login

The exp method returns the parameter power of the constant e.

Math.exp(1) // 2.718281828459045 Math.exp(3) // 20.085536923187668
Copy after login

random method

This method returns a pseudo-random number between 0 and 1, which may be equal to 0, but must be less than 1.

Math.random() // 0.7151307314634323 // 返回给定范围内的随机数 function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } // 返回给定范围内的随机整数 function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
Copy after login

Trigonometric function method

The sin method returns the sine of the parameter, the cos method returns the cosine of the parameter, and the tan method returns the tangent of the parameter.

Math.sin(0) // 0 Math.cos(0) // 1 Math.tan(0) // 0
Copy after login

The asin method returns the arc sine of the parameter, the acos method returns the arc cosine of the parameter, and the atan method returns the arc tangent of the parameter. The return values of these three methods are all radian values.

Math.asin(1) // 1.5707963267948966 Math.acos(1) // 0 Math.atan(1) // 0.7853981633974483
Copy after login

[Related recommendations:javascript video tutorial,web front-end

The above is the detailed content of Let's briefly talk about JavaScript's Math object method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!