Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Math object in JavaScript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Math objects in JavaScript. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Math object

Properties:

Math object methods:

Trigonometric functions (sin(), cos(), tan(),asin(), acos(), atan(), atan2()) return values in radians. Radians can be converted to angles by dividing Math.PI / 180, or by other methods.

Math.abs(x) returns the absolute value of x.Math.acos(x) returns the arccosine of x. Math. acosh(x) returns the archyperbolic cosine of x. Math.asin(x) returns the arcsine of x.Math. asinh(x) returns the archyperbolic sine of x. Math.atan(x) returns the arctangent of x in radians between-PI/2 and PI/2.Math.atanh(x) returns the archyperbolic tangent of x. Math. atan2 (x, y) Returns the arctangent of y/x.Math.cbrt(x) Returns the cube root of x. Math. ceiling (x) Returns the rounded up value of x. Math.clz32 (x)Returns the number of leading zeros of a 32-bit integer.Math.cos(x) Returns the cosine of x.Math.cosh(x) Returns the hyperbolic cosine of x.Math.exp(x) Returns Ex, where x is an argument and E is Euler's constant (2.718…), base of natural logarithm. Math.expm1 (x) returns exp(x)-1.Math.floor(x) returns the largest integer less than x. Math.fround(x)Returns the nearest single precision float representation of a number.Math.hypot ([x[,y[,…]]])Returns the square root of the sum of squares of its arguments.Math.imul(x)Returns the result of a 32-bit integer multiplication.Math.log(x)Returns the natural logarithm (loge, also ln) of a number.Math.log1p(x)Returns the natural logarithm of 1 + x (loge, also ln) of a number.Math.log10(x)Returns the base 10 logarithm of x.Math.log2(x)Returns the base 2 logarithm of x.Math.max ([x[,y[,…]]]) Returns 0 to the maximum of multiple values.Math.min([x[,y[,…]]]) returns 0 to the minimum of multiple values.Math.pow(x,y) returns x to the power y.Math.random() returns a pseudo-random number between 0 and 1. Math. round (x) returns a rounded integer. Math. sin (x) returns the sine of x. Math.sinh (x) returns the hyperbolic sine of x. Math.sqrt(x) returns the square root of x.Math.tan (x) returns the tangent of x. Math. tanh (x) returns the hyperbolic tangent of x. Math.toSource() returns the string "Math.trunc(x) returns the integer part of x, minus the decimal.

Example 1: Write a function that returns random integers from min to max, including min but excluding max

function getRandomArbitrary(min, max) { return min + Math.random() * (max - min);}

Example 2: Write a function that generates a random string of length n, with values ranging from 0 to 9, a to z, and A to Z.

function getRandomInt(min, max) { return min + Math.floor(Math.random() * (max - min + 1));}function randomStr(n){ var dict = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var str = ''; for(i = 0; i < n;i++){ str += dict[getRandomInt(0,62)]; } return str;}var str = getRandomStr (10);console.log(str); Thank you for reading this article carefully, I hope Xiaobian shared "How to use Math objects in JavaScript" This article is helpful to everyone, but also hope that everyone supports more, pay attention to industry information channels, more relevant knowledge waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report