# Introduction to basic methods of math built-in objects in JavaScript

2022-04-29 13:05:2351CTO

### Concept

• Math yes javaScript Built in objects for , It includes some mathematical constant attributes and mathematical function methods .

• Math It's not a function object , user Number Type , I won't support it BigInt.

• Math All properties and methods of are static .

• For example, when we use pi , It's written in Math.PI

• When using the sine and cosine function, the expression is Math.sin(x),x Is the parameter to pass in .

• Math The constant used is JavaScript Defined by the full precision floating-point number in .

### math Native properties

``````
//  Euler constant , It is also the base of natural logarithm , About equal to  2.718.
console.log("Math.E", Math.E);  //  Math.E 2.718281828459045
// 2  The natural logarithm of , About equal to  0.693.
console.log("Math.LN2", Math.LN2);  //  Math.LN2 0.6931471805599453
// 10  The natural logarithm of , About equal to  2.303.
console.log("Math.LN10", Math.LN10);  //  Math.LN10 2.302585092994046
//  With  2  At the bottom of the  E  The logarithmic , About equal to  1.443.
console.log("Math.LOG2E", Math.LOG2E);  //  Math.LOG2E 1.4426950408889634
//  With  10  At the bottom of the  E  The logarithmic , About equal to  0.434.
console.log("Math.LOG10E", Math.LOG10E);  //  Math.LOG10E 0.4342944819032518
//  PI , The ratio of the circumference of a circle to its diameter , About equal to  3.14159.
console.log("Math.PI", Math.PI);  //  Math.PI 3.141592653589793
//  Calculate the circumference of the circle
return 2 * Math.PI * radius;
}
console.log("calculateCircumference(1)", calculateCircumference(1)); // calculateCircumference(1) 6.283185307179586
//  A half  ½  The square root of , It's also  2  The reciprocal of the square root of   1 2 , About equal to  0.707.
console.log("Math.SQRT1_2", Math.SQRT1_2);  //  Math.SQRT1_2 0.7071067811865476
// 2  The square root of , About equal to  1.414.
console.log("Math.SQRT2", Math.SQRT2);  //  Math.SQRT2 1.4142135623730951

```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
```

### math Common methods

``````Math.abs()  //  Specify the number  “x“  The absolute value of
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN

```

1.
2.
3.
4.
5.
6.
7.
```

#### math Digital processing methods in daily development

``````// Math.round()  Function returns the nearest integer to which a number is rounded .
console.log(Math.round(20.49)); //20
console.log(Math.round(20.5)); //21
console.log(Math.round(-20.5)); //-20
console.log(Math.round(-20.51)); //-21

// Math.ceil()  Returns the smallest integer greater than or equal to a given number , Rounding up .
console.log(Math.ceil(0.95));
// 1
console.log(Math.ceil(4));
// 4
console.log(Math.ceil(7.004));
// 8
console.log(Math.ceil(-7.004));
// -7

// Math.floor()  Returns the maximum integer less than or equal to a given number , Math.floor() To round down .
Math.floor(45.95);
// 45
Math.floor(45.05);
// 45
Math.floor(4);
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46

// Math.max()  Returns the maximum value of a set of numbers
console.log(Math.max(1, 3, 2));
// 3
console.log(Math.max(-1, -3, -2));
// -1
const array1 = [1, -3, 2];
console.log(Math.max(...array1));
// 3

// Math.min()  Returns the minimum value of zero or more values .
console.log(Math.min()); // Infinity
console.log(Math.min(1, 2, 3, -4)); // -4

//  Use  Math.min()  Crop value （Clipping a value）
function f(x) {
if (x > 5) {
return (x = 5);
}
return (x = 6);
}
var finalMin = Math.min(f(2), 2, 3, 4, 5, 30);
console.log("finalMin", finalMin);  // 2

// Math.sqrt()  Returns the square root of a number
function calcHypotenuse(a, b) {
return Math.sqrt(a * a + b * b);
}
console.log(calcHypotenuse(3, 4));
// 5
console.log(calcHypotenuse(5, 12));
// 13
console.log(calcHypotenuse(0, 0));
// 0

```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
```
##### Use Math.random() Generate random number
``````/**
*
* Math.random()  Function returns a floating point number
*  Pseudo random numbers in the range from 0 To less than 1, in other words , from 0（ Include 0） Upward , But not including 1（ exclude 1）,
*  Then you can zoom to the desired range . Implement the initial seed selection to the random number generation algorithm ; It cannot be selected or reset by the user .
*
* */

console.log(Math.random());

function getRandomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; // Including the maximum value , Including the minimum value
}

console.log(getRandomNumber(2, 100));

```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
```

#### Summary

• The above examples include math Common methods and properties api
• math In use , Can combine random as well as max and min Such method , Generate the required random number
• adopt round、floor、ceil, We can take further values for numbers , Get a number format that meets the requirements

Math file

##### Mathjs plug-in unit

https://mathjs.org/examples/index.html

• mathjs The method provided by the plug-in is relatively comprehensive , It covers everything from algebraic calculations to functional calculations , Money calculation and other methods , Matrix serialization, etc , More ways to view official documents .

• Basic usage ：

``````npm install mathjs

import { sqrt } from 'mathjs'

console.log(sqrt(-4).toString()) // 2i

```

1.
2.
3.
4.
5.
6.
```