SoFunction
Updated on 2025-03-10

Partial extension of Math object in ES6

1、()

This method is used to take out the decimal part of a decimal and return the integer part. Let's see an example:

(1.234);  //1
(-2.34141);  //-2
(3.9);  //3
//For non-numeric values, the number will be used internally to convert it into a numeric value("12.87656");  //12
//For null values ​​and values ​​that cannot be intercepted, return NaN("a");  //NaN
();  //NaN
(NaN);  //NaN

2、()

This method is used to determine whether a number is a positive, negative, or zero. There are five return values. See the following example:

(23.235);  //1
(25);  //1
(0);  //0
(0.0);  //0
(-0.0);  //-0
(-0);  //-0
(-2);  //-1
(-2.983958);  //-1
('a');  //NaN
();  //NaN
(NaN);  //NaN

When the parameter is a positive number, return 1;

When the parameter is 0, return 0;

When the parameter is -0, return -0;

When the parameter is negative, return -1;

When the parameter is another value, NaN is returned.

3、()

This method is used to calculate the cubic root of a number, equivalent to the (n,1/3) method.

(8);  //2
(-64);  //-4
//For non-numeric values, the method also uses the Number method to convert it into a numeric value first, and then calculate it("125");  //5
("a");  //NaN

4、()

This method is used to calculate the square root of the sum of squares of all parameters.

(3,4);   //5
(1,2,3);  //3.741657386773941
(-5);  //5
();  //0
(NaN);  //NaN
("a");  //NaN
(3,'4');  //5
(3,'a');  //NaN

The above methods can greatly simplify the code and are very convenient.

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!