JavaScript - tutorial - 15 - numbers and math

revision:


Content

JavaScript has only one type of number. JavaScript number methods and properties. Math object Math object methods and properties summary of math object properties numbers_math examples


JavaScript has only one type of number.

top

Numbers can be written with or without decimals.

Extra large or extra small numbers can be written with scientific (exponent) notation.

Examples: numbers





code:
                    <div>
                        <a id="num_1a"></a><br><br>
                        <a id="num_1b"></a><br><br>
                    </div>
                    <script>
                        let xa = 3.14;
                        let ya = 3;
                        document.getElementById("num_1a").innerHTML = xa + "<br>" + ya;
        
                        let xb = 123e5;
                        let yb = 123e-5;
                        document.getElementById("num_1b").innerHTML = xb + "<br>" + yb;
                    </script>
                

The basics of numbers

1 - JavaScript numbers are always 64-bit floating point:

JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63.

2 - Integer precision : integers (numbers without a period or exponent notation) are accurate up to 15 digits. The maximum number of decimals is 17.

3 - Floating precision : floating point arithmetic is not always 100% accurate. To solve the problem, it helps to multiply and divide.

4 - Adding numbers and strings : If you add two numbers, the result will be a number; if you add two strings, the result will be a string concatenation; If you add a number and a string, the result will be a string concatenation; if you add a string and a number, the result will be a string concatenation:.

5 - Decimal base exponents: the format that can be used is xey , where "x" represents the first number, "e" indicates that zeroes follow "x", and "y" represents the number of zeroes.


longhand

                    for (let i = 0; i < 10000; i++) {}
                

shorthand

                    for (let i = 0; i < 1e7; i++) {}

                    1e1 === 10
                    1e2 === 100
                    1e3 === 1000
                    .
                    .
                    .
                    1e9 === 1000000000
                

Numbers and their specifics

1 - Numeric strings: JavaScript strings can have numeric content and JavaScript will try to convert strings to numbers in all numeric operations.

2 - NaN : “Not-a-Number” value : NaN is a JavaScript reserved word indicating that a number is not a legal number. Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number). You can use the global JavaScript function isNaN() to find out if a value is a not a number. If you use NaN in a mathematical operation, the result will also be NaN.

3 - Infinity : Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number. Division by 0 (zero) also generates Infinity. Infinity is a number: typeof Infinity returns number.

4 - Hexadecimal : JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x. By default, JavaScript displays numbers as base 10 decimals. But you can use the toString() method to output numbers from base 2 to base 36. Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.

5 - JavaScript numbers as objects : normally JavaScript numbers are primitive values created from literals (e.g. let x = 123;), but numbers can also be defined as objects with the keyword "new". It is advised not to create Number objects, as the "new" keyword complicates the code and slows down execution speed. Number Objects can also produce unexpected results.

Examples





















code:
                    <div>
                        <a id="num_1c"></a><br><br>
                        <a id="num_1d"></a><br><br>
                        <a id="num_1e"></a><br><br>
                        <a id="num_1f"></a><br><br>
                        <a id="num_1g"></a><br><br>
        
                        <a id="num_1h"></a><br><br>
                        <a id="num_1i"></a><br><br>
                        <a id="num_1j"></a><br><br>
                        <a id="num_1k"></a><br><br>
                        <a id="num_1l"></a><br><br>
                    </div>
                    <script>
                        let xc = 10; let yc = 20;
                        let zc = xc + yc;
                        document.getElementById("num_1c").innerHTML = "number(10) + number(20) = " + zc;
        
                        let xd = "10"; let yd = "20";
                        let zd = xd + yd;
                        document.getElementById("num_1d").innerHTML = "string('10') + string('20') = " + zd;
        
                        let xe = 10; let ye = "20";
                        let ze = xe + ye;
                        document.getElementById("num_1e").innerHTML = "number(10) + string('20') = "  + ze;
        
                        var xf= 10; var yf= 20;
                        document.getElementById("num_1f").innerHTML = "The result is: " + xf + yf;
        
                        let xg = "100"; let yg= "10"; let zg = xg / yg;   
                        document.getElementById("num_1h").innerHTML = "string('100') / string('10') =" + zg;
                    
                        let xh = "100"; let yh= "10"; let zh = xh * yh;   
                        document.getElementById("num_1i").innerHTML = "string('100') * string('10') =" + zh;
        
                        let xi = "100"; let yi= "10"; let zi = xi - yi;   
                        document.getElementById("num_1j").innerHTML = "string('100') - string('10') =" + zi;
        
                        let xj = "100"; let yj= "10"; let zj = xj + yj;   
                        document.getElementById("num_1k").innerHTML = "string('100') + string('10') =" + zj;
                        //In the last example JavaScript uses the + operator to concatenate the strings.
                    </script>
                
















code:
                    <div>
                        <a id="num_1m"></a><br><br>
                        <a id="num_1n"></a><br><br>
                        <a id="num_1o"></a><br><br>
                        <a id="num_1p"></a><br><br>
                        <a id="num_1q"></a><br><br>
        
                        <a id="num_1r"></a><br><br>
                        <a id="num_1s"></a><br><br>
                        <a id="num_1t"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("num_1m").innerHTML = "100/Apple =" + (100 / "Apple");
                        
                        document.getElementById("num_1n").innerHTML = "100/'10' ="  + (100 / "10");
                        
                        let xk = 100 / "Apple"; document.getElementById("num_1o").innerHTML = "isNaN : "  + isNaN(xk);
        
                        let xl = NaN; let yl = 5; document.getElementById("num_1p").innerHTML ="NaN + 5 = " + xl + yl;
        
                        let xm = NaN; document.getElementById("num_1q").innerHTML = "typeof NaN :"  + typeof xm;
        
                        let myNumber = 2; let txt = "";
                        while (myNumber != Infinity) {
                            myNumber = myNumber * myNumber;
                            txt = txt + myNumber + " , ";
                        }
                        document.getElementById("num_1r").innerHTML = "my number : " + txt;
        
                        let xn = 2/0; let yn = -2/0; document.getElementById("num_1s").innerHTML = "number : " + xn + "<br>" + yn;
        
                        document.getElementById("num_1t").innerHTML = typeof Infinity;
                    </script>
                














Comparing two JavaScript objects always returns false.

code:
                    <div>
                        <a id="num_1u"></a><br><br>
                        <a id="num_1v"></a><br><br>
                        <a id="num_1w"></a><br><br>
        
                        <a id="num_1x"></a><br><br>
                        <a id="num_1y"></a><br><br>
                        <a id="num_1z"></a><br><br>
                        <a id="num_2a"></a><br><br>    
                        <p>Comparing two JavaScript objects always returns false.</p>        
                    </div>
                    <script>
                        let xo = 0xFF; document.getElementById("num_1u").innerHTML = "0xFF = " + xo;
        
                        let myNumber1 = 32;
                        document.getElementById("num_1v").innerHTML = "Decimal 32 = " + "<br><br>" + 
                        "Hexatrigesimal (base 36): " + myNumber1.toString(36) + "<br>" +
                        "Duotrigesimal (base 32): " + myNumber1.toString(32) + "<br>" +
                        "Hexadecimal (base 16): " + myNumber1.toString(16) + "<br>" +
                        "Duodecimal (base 12): " + myNumber1.toString(12) + "<br>" +
                        "Decimal (base 10): " + myNumber1.toString(10) + "<br>" +
                        "Octal (base 8): " + myNumber1.toString(8) + "<br>" +
                        "Binary (base 2): " + myNumber1.toString(2);
        
                        // xp is a number
                        let xp = 123;
                        // yp is a Number object
                        let yp = new Number(123);
                        document.getElementById("num_1x").innerHTML = "typeof : " + typeof xp + "<br>" + typeof yp;
        
                        // xq is a number
                        let xq = 500;
                        // yq is a Number object
                        let yq = new Number(500);
                        document.getElementById("num_1y").innerHTML = "equals? " + (xq == yq);
                        
                        // xr is an object
                        let xr = new Number(500);
                        // yr is an object
                        let yr = new Number(500);
                        document.getElementById("num_1z").innerHTML = "equals? " + (xr == yr);
        
                        // xs is an object
                        let xsr = new Number(500);
                        // ys is an object
                        let ys = new Number(500);
                        document.getElementById("num_2a").innerHTML = "equals? " + (xr === yr);
                    </script>
                

Number techniques

Check if a number is even or odd: this task can be solved by using the modulo operator (%).

Example - code

            const isEven = num => num % 2 === 0;
            console.log(isEven(2)); 
            // Result: true
        
            const isEven = num => num % 2 === 0;
            console.log(isEven(3)); 
            // Result: false
        
            const result = (number % 2  == 0) ? "even" : "odd";
            console.log(`The number is ${result}.`); 
            // display the result
        

Generate a random number between two numbers: this will take two numbers as params and will generate a random number between those two numbers!

Example - code

                const random = (min, max) => 
                Math.floor(Math.random() * (max - min + 1) + min);
                console.log(random(1, 50)); 
                // could be anything from 1 - 50
            
                const randomFloat = (min, max) => 
                Math.random() * (max - min) + min;
            
                const randomInteger = (min, max) => 
                Math.floor(Math.random() * (max - min + 1)) + min;
            

Convert a string to a number: a very straight forward way of converting a string to a number using type coercion.

Example - code

            const toNumber = str => +str;
            //example
            toNumber('42');     
            // 42
        
            // Convert a string to a number explicitly
            toNumber = str => Number(str);
            toNumber("2"); 
            // Result: 2
        
            const toNumber = str => parseInt(str, 10);
            //example
            toNumber('42');    
            // 42
        

JavaScript number methods and properties.

top

constructor : returns the function that created JavaScript's Number prototype

Syntax: number.constructor

Parameters: none

For JavaScript numbers the constructor property returns:function Number() { [native code] }

examples
code;
                    <div>
                        <a id="num_2b"></a>
                    </div>
                    <script>
                        let num = 134.5; let text = num.constructor;
                        document.getElementById("num_2b").innerHTML = text;
                    </script>
                

Number.EPSILON : returns the difference between the smallest floating point number greater than 1 and 1.

Syntax: Number.EPSILON

Parameters: none

Number.EPSILON has the value of 2.220446049250313e-16. EPSILON is a property of the JavaScript Number object. You can only use it as Number.EPSILON. Using x.EPSILON, where x is a variable, will return undefined.

examples
code;
                    <div>
                        <a id="num_2c"></a>
                    </div>
                    <script>
                        let xxa = Number.EPSILON;
                        document.getElementById("num_2c").innerHTML = xxa;
                    </script>
                

isFinite : checks whether a value is a finite number; returns true if a number is a finite number.

Syntax: Number.isFinite(value)

Parameters: value : Required.The value to be tested.

Infinite (not finite) numbers are Infinity, -Infinity, or NaN. Otherwise it returns false.

examples












code;
                    <div>
                        <a id="num_2d"></a><br><br>
                        <a id="num_2e"></a><br><br>
                        <a id="num_2f"></a><br><br>
                        <a id="num_2g"></a><br><br>
                        <a id="num_2h"></a><br><br>
                        <a id="num_2i"></a><br><br>
                    </div>
                    <script>
                        let result = Number.isFinite(123);
                        document.getElementById("num_2d").innerHTML = result;
            
                        let result1 = Number.isFinite("123");
                        document.getElementById("num_2e").innerHTML = result;
            
                        document.getElementById("num_2f").innerHTML = isFinite("123");
            
                        document.getElementById("num_2g").innerHTML = Number.isFinite("123");
            
                        let res = "";
                        res += Number.isFinite(+1.23) + ": +1.23<br>";
                        res += Number.isFinite(-1.23) + ": -1.23<br>";
                        res += Number.isFinite('2005/12/12') + ": '2005/12/12'<br>";
                        document.getElementById("num_2h").innerHTML = res;
            
                        let res1 = "";
                        res1 += Number.isFinite(5-2) + ": 5-2<br>";
                        res1 += Number.isFinite(5/2) + ": 5/2<br>";
                        res1 += Number.isFinite(0) + ": 0<br>";
                        res1 += Number.isFinite(0/0) + ": 0/0<br>";
                        res1 += Number.isFinite(Infinity) + ": Infinity<br>";
                        res1 += Number.isFinite(-Infinity) + ": -Infinity<br>";
                        res1 += Number.isFinite(NaN) + ": NaN<br>";
                        document.getElementById("num_2i").innerHTML = res1;            
            
                    </script>
                

isInteger : checks whether a value is an integer; returns true if a value is an integer of the datatype Number.

Syntax: Number.isInteger(value)

Parameters: value : Required.The value to be tested.

examples








code;
                    <div>
                        <a id="num_2j"></a><br><br>
                        <a id="num_2k"></a><br><br>
                        <a id="num_2l"></a><br><br>
                        <a id="num_2m"></a><br><br>
                    </div>
                    <script>
                        let result2=
                        "Is 123 an integer? " + Number.isInteger(123) + "<br>" +
                        "Is 123 an integer? " + Number.isInteger(-123) + "<br>" +
                        "Is '123' an integer? " + Number.isInteger('123');
                        document.getElementById("num_2j").innerHTML = result2;
            
                        let result3 =
                        "Is 4-2 an integer? " + Number.isInteger(4-2) + "<br>" +
                        "Is 4/2 an integer? " + Number.isInteger(4/2) + "<br>" +
                        "Is 5-2 an integer? " + Number.isInteger(5-2) + "<br>" +
                        "Is 5/2 an integer? " + Number.isInteger(5/2);
                        document.getElementById("num_2k").innerHTML = result3;
            
                        let result4 =
                        "Is 0 an integer? " + Number.isInteger(0) + "<br>" +
                        "Is 0/0 an integer? " + Number.isInteger(0/0) + "<br>" +
                        "Is 0.5 an integer? " + Number.isInteger(0.5) + "<br>" +
                        "Is false an integer? " + Number.isInteger(false) + "<br>" +
                        "Is Infinity an integer? " + Number.isInteger(Infinity) + "<br>" +
                        "Is -Infinityo an integer? " + Number.isInteger(-Infinity) + "<br>" +
                        "Is NaN an integer? " + Number.isInteger(NaN);;
                        document.getElementById("num_2l").innerHTML = result4;
            
                    </script>
                

isNaN : checks whether a value is Number.NaN

Syntax: Number.isNaN(value)

Parameters: value : required. The value to be tested.

In JavaScript, NaN is short for "Not-a-Number". NaN is a number that is not a legal number. The Number.isNaN() method returns true if the value is NaN, and the type is a Number.

examples








code;
                    <div>
                        <a id="num_2n"></a><br><br>
                        <a id="num_2o"></a><br><br>
                        <a id="num_2p"></a><br><br>
                        <a id="num_2q"></a><br><br>
                    </div>
                    <script>
                        let result5 =
                        "Is 123 NaN? " + Number.isNaN(123) + "<br>" +
                        "Is -123 NaN? " + Number.isNaN(-1.23) + "<br>" +
                        "Is '123' NaN? " + Number.isNaN('123') + "<br>" +
                        "Is 0/0 NaN? " + Number.isNaN(0 / 0);
                        document.getElementById("num_2n").innerHTML = result5;
            
                        let result6 =
                        "Is 5-2 NaN? " + Number.isNaN(5-2) + "<br>" +
                        "Is 0 NaN? " + Number.isNaN(0) + "<br>" +
                        "Is 'Hello' NaN? " + Number.isNaN('Hello') + "<br>" +
                        "Is '2005/12/12\ NaN? " + Number.isNaN('2005/12/12') + "<br>" +
                        "Is ' ' NaN? " + Number.isNaN('');
                        document.getElementById("num_2o").innerHTML = result6;
            
                        let text1 = "Hello";
                        document.getElementById("num_2p").innerHTML = isNaN(text1);
            
                        let text2 = "Hello";
                        document.getElementById("num_2q").innerHTML = Number.isNaN(text2);
                    </script>
                

isSafeInteger : checks whether a value is a safe integer; returns true if a number is a safe integer.

Syntax: Number.isSafeInteger(value)

Parameters: value : required; the value to be tested.

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number: all integers from (253 - 1) to -(253 - 1).

examples








code;
                    <div>
                        <a id="num_2r"></a><br><br>
                        <a id="num_2s"></a><br><br>
                        <a id="num_2t"></a><br><br>
                        <a id="num_2u"></a><br><br>
                    </div>
                    <script>
                        let result7 =
                        "Is 123 a safe integer? " + Number.isSafeInteger(123) + "<br>" +
                        "Is +123 a safe integer? " + Number.isSafeInteger(-123) + "<br>" +
                        "Is '123' a safe integer? " + Number.isSafeInteger('123');
                        document.getElementById("num_2r").innerHTML = result7;
            
                        let result8 =
                        "Is 5-2 a safe integer? " + Number.isSafeInteger(5-2) + "<br>" +
                        "Is 0 a safe integer? " + Number.isSafeInteger(0) + "<br>" +
                        "Is 0.5 a safe integer? " + Number.isSafeInteger(0.5) + "<br>" +
                        "Is 0/0 a safe integer? " + Number.isSafeInteger(0/0);
                        document.getElementById("num_2s").innerHTML = result8;
            
                        let result9 =
                        "Is 2<sup>53</sup> a safe integer? " + Number.isSafeInteger(Math.pow(2, 53)) + "<br>" +
                        "Is 2<sup>53</sup> - 1 a safe integer? " + Number.isSafeInteger(Math.pow(2, 53) - 1);  
                        document.getElementById("num_2t").innerHTML = result9;       
                        
                        let result10 =
                        "Is true a safe integer? " + Number.isSafeInteger(true) + "<br>" +
                        "Is false a safe integer? " + Number.isSafeInteger(false) + "<br>" +
                        "Is Infinity a safe integer? " + Number.isSafeInteger(Infinity) + "<br>" +
                        "Is -Infinity a safe integer? " + Number.isSafeInteger(-Infinity);
                        document.getElementById("num_2u").innerHTML = result10;
                    </script>
                

MAX_SAFE_INTEGER : returns the maximum safe integer in JavaScript.

Syntax: Number.MAX_SAFE_INTEGER

Parameters: none

Number.MAX_SAFE_INTEGER is (253 - 1).

examples




code;
                    <div>
                        <a id="num_2v"></a><br><br>
                        <a id="num_2w"></a><br><br>
                    </div>
                    <script>
                        let xt = Number.MAX_SAFE_INTEGER;
                        document.getElementById("num_2v").innerHTML = xt;
                        let xu = 100; 
                        document.getElementById("num_2w").innerHTML = xu.MAX_SAFE_INTEGER;
                    </script>
                

MIN_SAFE_INTEGER : returns the minimum safe integer in JavaScript

Syntax: Number.MIN_SAFE_INTEGER

Parameters: none

Number.MIN_SAFE_INTEGER is -(253 - 1).

examples




code;
                    <div>
                        <a id="num_2x"></a><br><br>
                        <a id="num_2y"></a><br><br>
                    </div>
                    <script>
                        let xv = Number.MIN_SAFE_INTEGER;
                        document.getElementById("num_2x").innerHTML = xv;
                        let xw = 100; 
                        document.getElementById("num_2y").innerHTML = xw.MIN_SAFE_INTEGER;
                    </script>
                

MAX_VALUE : returns the largest number possible in JavaScript

Syntax: Number.MAX_VALUE

Parameters: none

Number.MAX_VALUE has the value of 1.7976931348623157e+308.

examples




code;
                    <div>
                        <a id="num_2z"></a><br><br>
                        <a id="num_3a"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("num_2z").innerHTML = Number.MAX_VALUE;
            
                        let xx = 100; 
                        document.getElementById("num_3a").innerHTML = xx.MAX_VALUE;
                    </script>
                

MIN_VALUE : returns the smallest number possible in JavaScript

Syntax: Number.MIN_VALUE

Parameters: none

Number.MIN_VALUE has a value of 5e-324.

examples




code;
                    <div>
                        <a id="num_3b"></a><br><br>
                        <a id="num_3c"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("num_3b").innerHTML = Number.MIN_VALUE;
            
                        let xy= 100; 
                        document.getElementById("num_3c").innerHTML = xy.MIN_VALUE+1;
                    </script>
                

NaN : represents a "Not-a-Number" value

Syntax: Number.NaN

Parameters: none

In JavaScript, NaN is short for "Not-a-Number". In JavaScript, NaN is a number that is not a legal number. The Global NaN property is the same as the Number.Nan property.

examples




code;
                    <div>
                        <a id="num_3d"></a><br><br>
                        <a id="num_3e"></a><br><br>
                    </div>
                    <script>
                        let xz = Number.NaN;
                        document.getElementById("num_3d").innerHTML = xz;
            
                        let xaa = NaN;
                        document.getElementById("num_3e").innerHTML = xaa;
                    </script>
                

NEGATIVE_INFINITY : represents negative infinity (returned on overflow)

Syntax: Number.NEGATIVE_INFINITY

Parameters: none

Number.NEGATIVE_INFINITY is "a number lower than any other number".

examples






code;
                    <div>
                        <a id="num_3f"></a><br><br>
                        <a id="num_3g"></a><br><br>
                        <a id="num_3h"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("num_3f").innerHTML = Number.NEGATIVE_INFINITY;
            
                        let n = (-Number.MAX_VALUE) * 2;
                        document.getElementById("num_3g").innerHTML = n;
            
                        let xab = 100;
                        document.getElementById("num_3h").innerHTML = xab.NEGATIVE_INFINITY;
                    </script>
                

POSITIVE_INFINITY : represents infinity (returned on overflow)

Syntax: Number.POSITIVE_INFINITY

Parameters: none

POSITIVE_INFINITY returns positive infinity. POSITIVE_INFINITY is "something higher than any other number".

examples






code;
                    <div>
                        <a id="num_3i"></a><br><br>
                        <a id="num_3j"></a><br><br>
                        <a id="num_3k"></a><br><br>
                    </div>
                    <script>
            
                        document.getElementById("num_3i").innerHTML = Number.POSITIVE_INFINITY;
            
                        let m = (-Number.MAX_VALUE) * 2;
                        document.getElementById("num_3j").innerHTML = m;
            
                        let xac = 100;
                        document.getElementById("num_3k").innerHTML = xac.POSITIVE_INFINITY;
                    </script>
                

parseFloat() : parses a string an returns a number; parses a value as a string and returns the first number.

Syntax: Number.parseFloat(value)

Parameters: value : required; the value to parse

If the first character cannot be converted, NaN is returned. Leading and trailing spaces are ignored. Only the first number found is returned.

examples




code;
                    <div>
                        <a id="num_3l"></a><br><br>
                        <a id="num_3m"></a><br><br>
                        
                    </div>
                    <script>
                        document.getElementById("num_3l").innerHTML =
                        Number.parseFloat(10) + "<br>" +
                        Number.parseFloat("10") + "<br>" +
                        Number.parseFloat("10.33") + "<br>" +
                        Number.parseFloat("34 45 66") + "<br>" +
                        Number.parseFloat("He was 40");
            
                        document.getElementById("num_3m").innerHTML =
                        Number.parseFloat("40.00") + "<br>" +
                        Number.parseFloat("   40   ") + "<br>" +
                        Number.parseFloat("40 years") + "<br>" +
                        Number.parseFloat("40H") + "<br>" +
                        Number.parseFloat("H40");
                    </script>
                

parseInt() : parses a string an returns a whole number; parses a value as a string and returns the first integer.

Syntax: Number.parseInt(string, radix)

Parameters: string : required; the string to be parsed; radix : optional. Default is 10; A number (2 to 36) specifying the number system.

A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.

examples




code;
                    <div>
                        <a id="num_3n"></a><br><br>
                        <a id="num_3o"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("num_3n").innerHTML =
                        Number.parseInt("10") + "<br>" +
                        Number.parseInt("10.00") + "<br>" +
                        Number.parseInt("10.33") + "<br>" +
                        Number.parseInt("34 45 66") + "<br>" +
                        Number.parseInt("   60   ") + "<br>" +
                        Number.parseInt("40 years") + "<br>" +
                        Number.parseInt("He was 40");
                        
                        document.getElementById("num_3o").innerHTML =
                        Number.parseInt("10", 10)+ "<br>" +
                        Number.parseInt("010")+ "<br>" +
                        Number.parseInt("10", 8)+ "<br>" +
                        Number.parseInt("0x10")+ "<br>" +
                        Number.parseInt("10", 16);
                    </script>
                

prototype : allows you to add properties and methods to an object

Syntax: Number.prototype.name = value

Parameters: none

prototype is a property available with all JavaScript objects. You are not advised to change the prototype of an object that you do not control. You should not change the prototype of built in JavaScript datatypes like: Numbers, Strings, Arrays, Dates, Booleans, Function, Objects. Only change the prototype of your own objects.

examples




code;
                    <div>
                        <a id="num_3p"></a><br><br>
                        <a id="num_3q"></a><br><br>
                        
                    </div>
                    <script>
                        Number.prototype.myMethod = function() {
                        return this.valueOf() / 2;
                        };
                        let o = 55;
                        document.getElementById("num_3p").innerHTML = o.myMethod();
            
                        function Person(first, last, age, eye) {
                            this.firstName = first;
                            this.lastName = last;
                            this.eyeColor = eye;
                        }
                        const myFather = new Person("John", "Doe", "blue");
                        const myMother = new Person("Sally", "Rally", "green");
                        Person.prototype.nationality = "English";
                        document.getElementById("num_3q").innerHTML = "My father is " + myFather.nationality + "<br>" +   "My mother is " + myMother.nationality;
                    </script>
                

toExponential(): converts a number into an exponential notation; returns a string with a rounded number written as exponential notation

Syntax: number.toExponential(x)

Parameters: x : optional. An integer between 0 and 20 representing the number of digits in the notation after the decimal point. If omitted, it is set to as many digits as necessary to represent the value.

examples




code;
                    <div>
                        <a id="num_3r"></a><br><br>
                        <a id="num_3s"></a><br><br>
                    </div>
                    <script>
                        let num1 = 5.56789;
                        let p = num1.toExponential();
                        document.getElementById("num_3r").innerHTML = p;
            
                        let num2 = 5.56789;
                        let q = num2.toExponential(3);
                        document.getElementById("num_3s").innerHTML = q;
                    </script>
                

toFixed(): formats a number with x numbers of digits after the decimal point; returns the string of a number with a specified number of decimals

Syntax: number.toFixed(x)

Parameters: x : optional. Number of decimals. Default is 0 (no decimals);

If the number of decimals are higher than in the number, zeros are added.

examples






code;
                    <div>
                        <a id="num_3t"></a><br><br>
                        <a id="num_3u"></a><br><br>
                        <a id="num_3v"></a><br><br>
                    </div>
                    <script>
                        let num3 = 5.56789;
                        let r = num3.toFixed();
                        document.getElementById("num_3t").innerHTML = r;
                        let s = num3.toFixed(2);
                        document.getElementById("num_3u").innerHTML = s;
                        let t = num3.toFixed(10);
                        document.getElementById("num_3v").innerHTML = t;
                    </script>
                

toLocaleString() : converts a number into a string, based on the locale settings; returns a number as a string, using local language format.

Syntax: number.toLocaleString(locales, options)

Parameters: locales : optional. The language specific format to use.; :

The language format depends on the locale setup on your computer.

examples






code;
                    <div>
                        <a id="num_3w"></a><br><br>
                        <a id="num_3x"></a><br><br>
                        <a id="num_3y"></a><br><br>
                    </div>
                    <script>
                        let num4 = 1000000;
                        let text4 = num4.toLocaleString();
                        document.getElementById("num_3w").innerHTML = text4;
                        let text5= num4.toLocaleString("fi-FI");
                        document.getElementById("num_3x").innerHTML = text5;
                        let text6 = num4.toLocaleString("en-US", {style:"currency", currency:"USD"});
                        document.getElementById("num_3y").innerHTML = text6;
                    </script>
                

locales options

examples

toLocaleString() returns a number as a string, using local language format.

code:
                    <div>
                        <p>toLocaleString() returns a number as a string, using local language format.</p>
                        <a id="num_3z"></a>
                    </div>
                    <script>
                        let u = 1000000;
                        let text7 = "";
                        text7 += "ar-SA: " + u.toLocaleString("ar-SA") + "<br>";
                        text7 += "bn-BD: " + u.toLocaleString("bn-BD") + "<br>";
                        text7 += "bn-IN: " + u.toLocaleString("bn-IN") + "<br>";
                        text7 += "cs-CZ: " + u.toLocaleString("cs-CZ") + "<br>";
                        text7 += "da-DK: " + u.toLocaleString("da-DK") + "<br>";
                        text7 += "de-AT: " + u.toLocaleString("de-AT") + "<br>";
                        text7 += "de-CH: " + u.toLocaleString("de-CH") + "<br>";
                        text7 += "de-DE: " + u.toLocaleString("de-DE") + "<br>";
                        text7 += "el-GR: " + u.toLocaleString("el-GR") + "<br>";
                        text7 += "en-AU: " + u.toLocaleString("en-AU") + "<br>";
                        text7 += "en-CA: " + u.toLocaleString("en-CA") + "<br>";
                        text7 += "en-GB: " + u.toLocaleString("en-GB") + "<br>";
                        text7 += "en-IE: " + u.toLocaleString("en-IE") + "<br>";
                        text7 += "en-IN: " + u.toLocaleString("en-IN") + "<br>";
                        text7 += "en-NZ: " + u.toLocaleString("en-NZ") + "<br>";
                        text7 += "en-US: " + u.toLocaleString("en-US") + "<br>";
                        text7 += "en-ZA: " + u.toLocaleString("en-ZA") + "<br>";
                        text7 += "es-AR: " + u.toLocaleString("es-AR") + "<br>";
                        text7 += "es-CL: " + u.toLocaleString("es-CL") + "<br>";
                        text7 += "es-CO: " + u.toLocaleString("es-CO") + "<br>";
                        text7 += "es-ES: " + u.toLocaleString("es-ES") + "<br>";
                        text7 += "es-MX: " + u.toLocaleString("es-MX") + "<br>";
                        text7 += "es-US: " + u.toLocaleString("es-US") + "<br>";
                        text7 += "fi-FI: " + u.toLocaleString("fi-FI") + "<br>";
                        text7 += "fr-BE: " + u.toLocaleString("fr-BE") + "<br>";
                        text7 += "fr-CA: " + u.toLocaleString("fr-CA") + "<br>";
                        text7 += "fr-CH: " + u.toLocaleString("fr-CH") + "<br>";
                        text7 += "fr-FR: " + u.toLocaleString("fr-FR") + "<br>";
                        text7 += "he-IL: " + u.toLocaleString("he-IL") + "<br>";
                        text7 += "hi-IN: " + u.toLocaleString("hi-IN") + "<br>";
                        text7 += "hu-HU: " + u.toLocaleString("hu-HU") + "<br>";
                        text7 += "id-ID: " + u.toLocaleString("id-ID") + "<br>";
                        text7 += "it-CH: " + u.toLocaleString("it-CH") + "<br>";
                        text7 += "it-IT: " + u.toLocaleString("it-IT") + "<br>";
                        text7 += "ja-JP: " + u.toLocaleString("ja-JP") + "<br>";
                        text7 += "ko-KR: " + u.toLocaleString("ko-KR") + "<br>";
                        text7 += "nl-BE: " + u.toLocaleString("nl-BE") + "<br>";
                        text7 += "nl-NL: " + u.toLocaleString("nl-NL") + "<br>";
                        text7 += "no-NO: " + u.toLocaleString("no-NO") + "<br>";
                        text7 += "pl-PL: " + u.toLocaleString("pl-PL") + "<br>";
                        text7 += "pt-BR: " + u.toLocaleString("pt-BR") + "<br>";
                        text7 += "pt-PT: " + u.toLocaleString("pt-PT") + "<br>";
                        text7 += "ro-RO: " + u.toLocaleString("ro-RO") + "<br>";
                        text7 += "ru-RU: " + u.toLocaleString("ru-RU") + "<br>";
                        text7 += "sk-SK: " + u.toLocaleString("sk-SK") + "<br>";
                        text7 += "sv-SE: " + u.toLocaleString("sv-SE") + "<br>";
                        text7 += "ta-IN: " + u.toLocaleString("ta-IN") + "<br>";
                        text7 += "ta-LK: " + u.toLocaleString("ta-LK") + "<br>";
                        text7 += "th-TH: " + u.toLocaleString("th-TH") + "<br>";
                        text7 += "tr-TR: " + u.toLocaleString("tr-TR") + "<br>";
                        text7 += "zh-CN: " + u.toLocaleString("zh-CN") + "<br>";
                        text7 += "zh-HK: " + u.toLocaleString("zh-HK") + "<br>";
                        text7 += "zh-TW: " + u.toLocaleString("zh-TW") + "<br>";
                        document.getElementById("num_3z").innerHTML = text7;
                    </script>
                

toPrecision(): formats a number to x length; string of a number written with a specified length

Syntax: number.number.toPrecision(x)

Parameters: x : optional. The number of digits. If omitted, the number is returned without any formatting.

A decimal point and nulls are added (if needed), to create the specified length.

examples






code;
                    <div>
                        <a id="num_4a"></a><br><br>
                        <a id="num_4b"></a><br><br>
                        <a id="num_4c"></a><br><br>
                    
                    </div>
                    <script>
                        let num5 = 13.3714;
                        document.getElementById("num_4a").innerHTML = num5.toPrecision(2);
            
                        let num6 = 0.001658853;
                        document.getElementById("num_4b").innerHTML =
                        num6.toPrecision(2) + "<br>" +
                        num6.toPrecision(3) + "<br>" +
                        num6.toPrecision(10);
            
                        let num7 = 13.3714;
                        document.getElementById("num_4c").innerHTML =
                        num7.toPrecision(2) + "<br>" +
                        num7.toPrecision(3) + "<br>" +
                        num7.toPrecision(10);
                        num7.toPrecision();
                    </script>
                

toString(): converts a number to a string; returns a number as a string

Syntax: number.toString(radix)

Parameters: radix : optional. The base to use. Must be an integer between 2 and 36. Base 2 is binary. Base 8 is octal. Base 16 is hexadecimal.

examples








code;
                    <div>
                        <a id="num_4d"></a><br><br>
                        <a id="num_4e"></a><br><br>
                        <a id="num_4f"></a><br><br>
                        <a id="num_4g"></a><br><br>
                    </div>
                    <script>
                        let num8 = 15;
                        let text8 = num8.toString();
                        document.getElementById("num_4d").innerHTML = text8;
                        //convert a number to a string, using base 2 (binary):
                        let text8a = num8.toString(2);
                        document.getElementById("num_4e").innerHTML = text8a;
                        //convert a number to a string, using base 2 (binary):
                        let text8b = num8.toString(8);
                        document.getElementById("num_4f").innerHTML = text8b;
                        let text8c = num8.toString(16);
                        document.getElementById("num_4g").innerHTML = text8c;
                    </script>
                

valueOf(): returns the primitive value of a number; returns a number as a number

Syntax: number.valueOf()

Parameters: none

examples




code;
                    <div>
                        <a id="num_4i"></a><br><br>
                        <a id="num_4j"></a><br><br>
                    </div>
                    <script>
                        let num9 = 15;
                        let n9 = num9.valueOf();
                        document.getElementById("num_4i").innerHTML = n9;
                    </script>
                

Math object

top

The JavaScript math object is a top-level, predefined object for mathematical constants and functions.
Mathematical properties and functions can be calculated by "math.property" or math.method".

The Math object allows to perform mathematical tasks on numbers. Unlike other global objects, the Math object has no constructor.
Methods and properties are static. All methods and properties (constants) can be used without creating a Math object first.


Math object methods and properties

top

syntax for Math methods: Math.method(number)

abs(x): returns the absolute (positive) value of x

parameters: x: required; a number

examples
code:
                    <div class="spec">
                        <a id="math_1a"></a>       
                    </div>
                    <script>
                        document.getElementById("math_1a").innerHTML = "Math.abs(-6.4) = " + Math.abs(-6.4);
                    </script>
                

acos(x): the arccosine of x, in radians; it returns a value between 0 and PI

parameters: x: required; a number

examples



code:
                    <div>
                        <a id="math_1b"></a><br>  
                        <a id="math_1c"></a><br>  
                        <a id="math_1d"></a><br>   
                    </div>
                    <script>
                        document.getElementById("math_1b").innerHTML = Math.acos(-1);
                        document.getElementById("math_1c").innerHTML = Math.acos(0);
                        document.getElementById("math_1d").innerHTML = Math.acos(1);
                    </script>
                

acosh(x):eturns the hyperbolic arc-cosine of a number. If the parameter is less than 1, the method returns NaN.

parameters: x: required; a number

examples


code:
                    <div>
                        <a id="math_1e"></a><br>  
                        <a id="math_1f"></a><br>  
                        
                    </div>
                    <script>
                        document.getElementById("math_1e").innerHTML = Math.acosh(1);
                        document.getElementById("math_1f").innerHTML = Math.acosh(2);
                        
                    </script>
                

asin(x):arcsine of x, in radians

parameters: x: required; a number

examples



code:
                    <div>
                        <a id="math_1g"></a><br>  
                        <a id="math_1h"></a><br>  
                        <a id="math_1i"></a><br>  
                    </div>
                    <script>
                                document.getElementById("math_1g").innerHTML = Math.asin(-1);
                                document.getElementById("math_1h").innerHTML = Math.asin(0);
                                document.getElementById("math_1i").innerHTML = Math.asin(1);
                    </script>
                

asinh(x): returns the hyperbolic arc-sine of a number.

parameters: x: required; a number

examples


code:
                    div>
                        <a id="math_1j"></a><br>  
                        <a id="math_1k"></a><br>  
                    
                    </div>
                    <script>
                                document.getElementById("math_1j").innerHTML = Math.asinh(0);
                                document.getElementById("math_1k").innerHTML = Math.asinh(1);
                                
                    </script>
                

atan(x): the arctangent of x as a numeric value

parameters: x: required; a number;

examples


code:
                    <div>
                        <a id="math_1l"></a><br>  
                        <a id="math_1m"></a><br>  
                    </div>
                    <script>
                        document.getElementById("math_1l").innerHTML = Math.atan(2);
                        document.getElementById("math_1m").innerHTML = Math.atan(1);
                                
                    </script>
                

atan2(y,x): arctangent of the quotient of its arguments

parameters: x: required; a number; y : required; a number

examples


code:
                    <div>
                        <a id="math_1n"></a><br>  
                        <a id="math_1o"></a><br>  
                    </div>
                    <script>
                        document.getElementById("math_1n").innerHTML = Math.atan2(2,4);
                        document.getElementById("math_1o").innerHTML = Math.atan2(8,4);
                                
                    </script>
                

atanh(x): returns the hyperbolic arctangent of a number.

parameters: x: required; a number

examples


code:
                    <div>
                        <a id="math_1p"></a><br>  
                        <a id="math_1q"></a><br>  
                    </div>
                    <script>
                        document.getElementById("math_1p").innerHTML = Math.atanh(0.5);
                        document.getElementById("math_1q").innerHTML = Math.atanh(0.25);
                    </script>
                

cbrt(x): returns the cubic root of a number..

parameters: x: required; a number

examples


code:
                    <div>
                        <a id="math_1r"></a><br>  
                        <a id="math_1s"></a><br>  
                    </div>
                    <script>
                        document.getElementById("math_1r").innerHTML = Math.cbrt(25);
                        document.getElementById("math_1s").innerHTML = Math.cbrt(125);
                    </script>
                

ceil(x): value of x rounded up to its nearest integer

parameters: x: required; a number

examples
code:
                    <div class="spec">
                        <a id="math4"></a>         
                    </div>
                    <script>
                        document.getElementById("math_1t").innerHTML = "Math.ceil(6.4): " + Math.ceil(6.4);
                    </script>
                

clz32(x): returns the number of leading zeros in a 32-bit binary number.

parameters: x: required; a number

clz32 is short for CountLeadingZeroes32.

examples

code:
                    <div class="spec">
                        <a id="math_1u"></a><br> 
                        
                    </div>
                    <script>
                        let a = Math.clz32(0);
                        let b = Math.clz32(1);
                        let c = Math.clz32(2);
                        let d = Math.clz32(4);
                        document.getElementById("math_1u").innerHTML =   a + "<br>" + b + "<br>" + c + "<br>" + d;
                    </script>
                

cos(x): the cosine of x (x is in radians)

parameters: x: required; a number

If you want to use degrees instead of radians, you have to convert degrees to radians. Angle in radians = Angle in degrees x PI / 180.

examples




code:
                    <div class="spec">
                        <a id="math_1v"></a> <br><br>   
                        <a id="math_1w"></a><br><br>  
                        <a id="math_1x"></a>    
                    </div>
                    <script>
                        let x = Math.cos(3.14);
                        document.getElementById("math_1v").innerHTML = x.toFixed(2);
            
                        document.getElementById("math_1w").innerHTML =
                            "Cosine of 0 degrees is: " + Math.cos(0).toFixed(2) +
                            "<br>" +
                            "Cosine of 60 degrees is: " + Math.cos(Math.PI/3).toFixed(2) +
                            "<br>" +
                            "Cosine of 90 degrees is: " + Math.cos(Math.PI/2).toFixed(2) +
                            "<br>" +
                            "Cosine of 180 degrees is: " + Math.cos(Math.PI).toFixed(2) +
                            "<br>" +
                            "Cosine of 360 degrees is: " + Math.cos(Math.PI*2).toFixed(2);
            
                            document.getElementById("math_1x").innerHTML =
                                "Cosine of 0 degrees is: " + cosine(0) +
                                "<br>" +
                                "Cosine of 60 degrees is: " + cosine(60) +
                                "<br>" +
                                "Cosine of 90 degrees is: " + cosine(90) +
                                "<br>" +
                                "Cosine of 180 degrees is: " + cosine(180) +
                                "<br>" +
                                "Cosine of 240 degrees is: " + cosine(240) +
                                "<br>" +
                                "Cosine of 360 degrees is: " + cosine(360);
            
                            function cosine(degree) {
                                // radians = degrees * PI / 180
                                let x = Math.cos(degree * Math.PI / 180);
                                return x.toFixed(2);   
                            }
            
                    
                

cosh(x): returns the hyperbolic cosine of a number.

parameters: x: required; a number

examples

Click the button to display the hyperbolic cosine value of 3.


code:
                    <div>
                        <p>Click the button to display the hyperbolic cosine value of 3.</p>
                        <button onclick="coshFunction()">Try it</button>
                        <a id="math_1y"></a><br>
                    </div>
                    <script>
                        function coshFunction() {
                        document.getElementById("math_1y").innerHTML = Math.cosh(3);
                        }
                    </script>
                

Math E property: returns Euler's number.

The Math.E property returns approximately 2.718. Euler's number is the base of natural logarithms.

examples
code:
                    <div>
                        <a id="math_1z"></a>
                    </div>
                    <script>
                        document.getElementById("math_1z").innerHTML = Math.E;
                    </script>
                

exp(x): returns the value of Ex, where E is Euler's number (approximately 2.7183) and x is the number passed to it.

parameters: x: required; a number

examples




code:
                    <div>
                        <a id="math_2a"></a><br><br>
                        <a id="math_2b"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_2a").innerHTML = Math.exp(3);
                        let aa = Math.exp(1);
                        let ab = Math.exp(-1);
                        let ac = Math.exp(5);
                        let ad = Math.exp(10);
                        document.getElementById("math_2b").innerHTML =   aa + "<br>" + ab + "<br>" + ac + "<br>" + ad; 
                    </script>   
                

expm1(x): returns the value of Ex, where E is Euler's number (approximately 2.7183) and x is the number passed to it.

parameters: x: required; a number

This method is more accurate than using Math.exp() and subtracting 1.

examples




code:
                    <div>
                        <a id="math_2a"></a><br><br>
                        <a id="math_2b"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_2a").innerHTML = Math.exp(3);
                        let aa = Math.exp(1);
                        let ab = Math.exp(-1);
                        let ac = Math.exp(5);
                        let ad = Math.exp(10);
                        document.getElementById("math_2b").innerHTML =  aa + "<br>" + ab + "<br>" + ac + "<br>" + ad; 
                    </script>   
                

floor(x) : returns the value of x rounded down to its nearest integer.

parameters: x: required; a number

examples




code:
                    <div class="spec">
                        <a id="math_2e"></a><br><br>    
                        <a id="math_2f"></a><br><br>          
                    </div>
                    <script>
                        document.getElementById("math_2e").innerHTML = "Math.floor(4.7): " + Math.floor(4.7);
                        let ca = Math.floor(0.60);
                        let cb = Math.floor(0.40);
                        let cc = Math.floor(5);
                        let cd = Math.floor(5.1);
                        let ce = Math.floor(-5.1);
                        let cf = Math.floor(-5.9);
                        document.getElementById("math_2f").innerHTML = ca + "<br>" + cb + "<br>" + cc + "<br>" + cd + "<br>" + ce + "<br>" + cf;
            
                    </script>
                

fround(x): returns the nearest 32-bit single precision float representation of a number.

parameters: x: required; a number

examples




code:

                

Math.LN2 property: returns the natural logarithm of 2.

The Math.LN2 propery returns approximately 0.693.

examples
code:
                    <div>
                        <a id="math_2i"></a>
                    </div>
                    <script>
                        document.getElementById("math_2i").innerHTML = Math.LN2;
                    </script>
                

Math.LN10 property: returns the natural logarithm of 10.

The Math.LN10 property approximately 2.302.

examples
code:
                    <div>
                        <a id="math_2j"></a>
                    </div>
                    <script>
                        document.getElementById("math_2j").innerHTML = Math.LN10;
                    </script>
                

log(x): returns the natural logarithm (base E) of a number.

parameters: x: required; a number

examples






code:
                    <div>
                        <a id="math_2k"></a><br><br>
                        <a id="math_2l"></a><br><br>
                        <a id="math_2m"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_2k").innerHTML = Math.log(2);
                        let ea = Math.log2(2.7183);
                        let eb = Math.log2(2);
                        let ec = Math.log2(1);
                        let ed = Math.log2(0);
                        let ee = Math.log2(-1);
                        document.getElementById("math_2l").innerHTML = ea + "<br>" + eb + "<br>" + ec + "<br>" + ed + "<br>" + ee;
                    </script>
                

log10(x): returns the base-10 logarithm of a number.

parameters: x: required; a number

examples




code:
                    <div>
                        <a id="math_2n"></a><br><br>
                        <a id="math_2o"></a><br><br>
                        
                    </div>
                    <script>
                        document.getElementById("math_2n").innerHTML = Math.log(2);
                        let fa = Math.log10(2.7183);
                        let fb = Math.log10(2);
                        let fc = Math.log10(1);
                        let fd = Math.log10(0);
                        let fe = Math.log10(-1);
                        document.getElementById("math_2o").innerHTML = fa + "<br>" + fb + "<br>" + fc + "<br>" + fd + "<br>" + fe;
                    </script>
                

log1p(x): returns the natural logarithm (base E) of 1 + a number.

parameters: x: required; a number

examples




code:
                    <div>
                        <a id="math_2p"></a><br><br>
                        <a id="math_2q"></a><br><br>
                        
                    </div>
                    <script>
                        document.getElementById("math_2p").innerHTML = Math.log1p(2);
                        let ga = Math.log1p(2.7183);
                        let gb = Math.log1p(2);
                        let gc = Math.log1p(1);
                        let gd = Math.log1p(0);
                        let ge = Math.log1p(-1);
                        document.getElementById("math_2q").innerHTML = ga + "<br>" + gb + "<br>" + gc + "<br>" + gd + "<br>" + ge;
                    </script>
                

log2(x): returns the base-2 logarithm of a number.

parameters: x: required; a number

examples




code:
                    <div>
                        <a id="math_2k"></a><br><br>
                        <a id="math_2l"></a><br><br>
                        <a id="math_2m"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_2k").innerHTML = Math.log(2);
                        let ea = Math.log2(2.7183);
                        let eb = Math.log2(2);
                        let ec = Math.log2(1);
                        let ed = Math.log2(0);
                        let ee = Math.log2(-1);
                        document.getElementById("math_2l").innerHTML = ea + "<br>" + eb + "<br>" + ec + "<br>" + ed + "<br>" + ee;
                    </script>
                

Math.Log2E property: returns the base-2 logarithm of E.

The Math.LOG2E property returns approximately 1.442.

examples
code:
                    <div>
                        <a id="math_2t"></a>
                    </div>
                    <script>
                        document.getElementById("math_2t").innerHTML = Math.LOG2E;
                    </script>
                

Math.LOG10E property: returns the base-10 logarithm of E.

The Math.LOG10E property returns approximately 0.434.

examples
code:
                    <div>
                        <a id="math_2u"></a>
                    </div>
                    <script>
                        document.getElementById("math_2u").innerHTML = Math.LOG10E;
                    </script>
                

max(x,y,z,...,n): returns the number with the highest value

parameters: n1, n2, ..., nX: optional; one or more numbers to compare.

examples




code:
                    <div class="spec">
                        <a id="demo_b"></a>
                    </div>
                    <script>
                        document.getElementById("demo_b").innerHTML = Math.max(0, 150, 30, 20, -8, -200);
                    </script>
                

min(x,y,z,...,n): returns the number with the lowest value

parameters: n1, n2, ..., nX: optional; one or more numbers to compare.

examples




code:
                    <div class="spec">
                        <a id="math_2x"></a><br><br>
                        <a id="math_2y"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_2x").innerHTML = Math.min(0, 150, 30, 20, -8, -200);
            
                        let ja = Math.min(5, 10);
                        let jb = Math.min(0, 150, 30, 20, 38);
                        let jc = Math.min(-5, 10);
                        let jd = Math.min(-5, -10);
                        let je = Math.min(1.5, 2.5);
            
                        document.getElementById("math_2y").innerHTML = ja + "<br>" + jb + "<br>" + jc + "<br>" + jd + "<br>" + je;
                    </script>
                

Math.PI property: returns PI (the ratio of a circle's area to the square of its radius, approximately 3.14)

examples
code:
                    <div>
                        <a id="math_2z"></a>
                    </div>
                    <script>
                        document.getElementById("math_2z").innerHTML = Math.PI;
                    </script>
                

pow(x,y): returns the value of x to the power of y (xy).

parameters: x: required; a number

examples




code:
                    <div class="spec">
                        <a id="math_3a"></a><br><br>
                        <a id="math_3b"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_3a").innerHTML = "Math.pow(8,2): " + Math.pow(8,2);
            
                        let ka = Math.pow(0, 1);
                        let kb = Math.pow(1, 1);
                        let kc = Math.pow(1, 10);
                        let kd = Math.pow(3, 3);
                        let ke = Math.pow(-3, 3);
                        let kf = Math.pow(2, 4);
            
                        document.getElementById("math_3b").innerHTML = ka + "<br>" + kb + "<br>" + kc + "<br>" + kd + "<br>" + ke + "<br>" + kf;            
                    </script>
                

truncate a number to a fixed decimal point: using the Math.pow() method, we can truncate a number to a certain decimal point that we provide in the function. The built-in toFixed() method can easily do this conversion, but it gives weird results in some cases because of the way floating-point arithmetics work. To avoid this unexpected behavior in the code, the numbers can be represented in exponential notation and use Math.round() to get the decimal rounded to a given number of decimal places.

Example - code

                const toFixed = (n, fixed) => 
                ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
                // Examples
                toFixed(25.198726354, 1);       // 25.1
                toFixed(25.198726354, 2);       // 25.19
                toFixed(25.198726354, 3);       // 25.198
                toFixed(25.198726354, 4);       // 25.1987
                toFixed(25.198726354, 5);       // 25.19872
                toFixed(25.198726354, 6);       // 25.198726
            
                const round = (n, d) => 
                Number(Math.round(n + "e" + d) + "e-" + d)
                //examples
                round(1.005, 2) //1.01
                round(1.555, 2) //1.56
                
            
                const toFixed = (n, fixed) => 
                `${n}`.match(new RegExp(`^-?\\d+(?:\.\\d{0,${fixed}})?`))[0];
                // Examples
                toFixed(25.198726354, 1);       // 25.1
                toFixed(25.198726354, 2);       // 25.19
                toFixed(25.198726354, 3);       // 25.198
                toFixed(25.198726354, 4);       // 25.1987
                toFixed(25.198726354, 5);       // 25.19872
                toFixed(25.198726354, 6);       // 25.198726
            

random(): returns a random number between 0 and 1

parameters: none

examples












code:
                    <div class="spec">
                        <a id="math_3c"></a><br><br>
                        <a id="math_3d"></a><br><br>
                        <a id="math_3e"></a><br><br>
                        <a id="math_3f"></a><br><br>
                        <a id="math_3g"></a><br><br>
                        <a id="math_3h"></a><br><br>
            
                    </div>
                    <script>
                        document.getElementById("math_3c").innerHTML = "Math.random(): " + Math.random();
                        let ax = Math.random() * 10;
                        document.getElementById("math_3d").innerHTML = ax;
                        let bx = Math.random() * 100;
                        document.getElementById("math_3e").innerHTML = bx;
                        let cx = Math.random() * 1000 ;
                        document.getElementById("math_3f").innerHTML = cx;
                        let dx = Math.floor((Math.random() * 10) + 1);
                        document.getElementById("math_3g").innerHTML = dx;
                        let ex = Math.floor((Math.random() * 100) + 1);
                        document.getElementById("math_3h").innerHTML = ex;
            
                    
                

round(x): the value of x rounded to its nearest integer

parameters: x : required; a number

examples




code:
                    <div class="spec">
                        <a id="math_3i"></a><br><br>
                        <a id="math_3j"></a><br><br>       
                    </div>
                    <script>
                        document.getElementById("math_3i").innerHTML = "Math.round(4.4): " + Math.round(4.4);
            
                        let la = Math.round(2.60);
                        let lb = Math.round(2.50);
                        let lc = Math.round(2.49);
                        let ld = Math.round(-2.60);
                        let le = Math.round(-2.50);
                        let lf = Math.round(-2.49);
            
                        document.getElementById("math_3j").innerHTML = la + "<br>" + lb + "<br>" + lc + "<br>" + ld + "<br>" + le + "<br>" + lf;
                    </script>
                

sign(x): retuns whether a number is negative, positive or zero.

parameters: x : required; a number

If the number is positive, this method returns 1. If the number is negative, it returns -1. If the number is zero, it returns 0.

examples






code:
                    <div>
                        <a id="math_3k"></a><br><br>
                        <a id="math_3l"></a><br><br>   
                        <a id="math_3m"></a><br><br>   
                    </div>
                    <script>
                        document.getElementById("math_3k").innerHTML =  Math.sign(3);
                        document.getElementById("math_3l").innerHTML =  Math.sign(-3);
                        document.getElementById("math_3m").innerHTML =  Math.sign(0);
                    </script>
                

sin(x): the sine of x (x is in radians)

parameters: x : required; a number

examples








code:
                    <div class="spec">
                        <a id="math_3n"></a><br><br>
                        <a id="math_3o"></a><br><br>   
                        <a id="math_3p"></a><br><br>  
                        <a id="math_3q"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_3n").innerHTML = "The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);
                        let mx = Math.sin(3.14);
                        document.getElementById("math_3o").innerHTML = mx.toFixed(2);
                        document.getElementById("math_3p").innerHTML =
                            "Sine of 0 degrees is: " + Math.sin(0).toFixed(2) + 
                            "<br>" +
                            "Sine of 30 degrees is: " + Math.sin(Math.PI/6).toFixed(2) + 
                            "<br>" +
                            "Sine of 90 degrees is: " + Math.sin(Math.PI/2).toFixed(2) + 
                            "<br>" +
                            "Sine of 180 degrees is: " + Math.sin(Math.PI).toFixed(2) +
                            "<br>" +
                            "Sine of 210 degrees is: " + Math.sin(Math.PI*3/2).toFixed(2) +
                            "<br>" +
                            "Sine of 360 degrees is: " + Math.sin(Math.PI*2).toFixed(2);
                            
                        document.getElementById("math_3q").innerHTML =
                            "The sine of  0 degrees is: " +  sine(0) +
                            "<br>" +
                            "The sine of 30 degrees is: " +  sine(30) +
                            "<br>" +
                            "The sine of 90 degrees is: " +  sine(90) +
                            "<br>" +
                            "The sine of 150 degrees is: " +  sine(150) +
                            "<br>" +
                            "The sine of 180 degrees is: " +  sine(180) +
                            "<br>" +
                            "The sine of 210 degrees is: " +  sine(210) +
                            "<br>" +
                            "The sine of 270 degrees is: " +  sine(270) +
                            "<br>" +
                            "The sine of 360 degrees is: " +  sine(360);
            
                        function sine(degree) {
                            // radians = degrees * PI / 180
                            let x = Math.sin(degree * Math.PI / 180);
                            return x.toFixed(2);   
                        }
                    </script>
                

sinh(x): returns the hyperbolic sine of a number.

parameters: x : required; a number

examples


code:
                    <div class="spec">
                        <a id="math_3r"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_3r").innerHTML = Math.sinh(3);
                    </script>
                

sqrt(x): returns the square root of a number.

parameters: x : required; a number

example





code:
                    <div class="spec">
                        <a id="math_3s"></a><br><br>
                        <a id="math_3t"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_3s").innerHTML = "Math.sqrt(64): " + Math.sqrt(64);
            
                        let na = Math.sqrt(0);
                        let nb = Math.sqrt(1);
                        let nc = Math.sqrt(9);
                        let nd = Math.sqrt(64);
                        let ne = Math.sqrt(-9);
            
                        document.getElementById("math_3t").innerHTML = na + "<br>" + nb + "<br>" + nc + "<br>" + nd + "<br>" + ne;
                    </script>
                

Math.SQRT1_2 property: returns the square root of 1/2.

The Math.SQRT1_2 property returns approximately 0.707.

examples
code:
                    <div>
                        <a id="math_3u"></a>
                    </div>
                    <script>
                        document.getElementById("math_3u").innerHTML = Math.SQRT1_2;
                    </script>
                

Math.SQRT2 property: returns the square root of 2.

The Math.SQRT2 property returns approximately 1.414.

examples
code:
                    <div>
                        <a id="math_3u"></a>
                    </div>
                    <script>
                        document.getElementById("math_3u").innerHTML = Math.SQRT1_2;
                    </script>
                

tan(x): returns the tangent of a number.

parameters: x : required; a number

the Math.tan() method expects the number in radians.

examples






code:
                    <div>
                        <a id="math_3w"></a><br><br>
                        <a id="math_3x"></a><br><br>
                        <a id="math_3y"></a><br><br>
                    
                    </div>
                    <script>
                        let radians = 2;
                        document.getElementById("math_3w").innerHTML = Math.tan(radians);
                    
                        let pa = Math.tan(0).toFixed(2);
                        let pb = Math.tan(1).toFixed(2);
                        let pc = Math.tan(2).toFixed(2);
                        let pd = Math.tan(3).toFixed(2);
                        document.getElementById("math_3x").innerHTML = pa + "<br>" + pb + "<br>" + pc + "<br>" + pd;
            
                        let degrees = 60;
                        let radians1 = degrees * Math.PI/180; 
                        document.getElementById("math_3y").innerHTML = Math.tan(radians1);
                    </script>
                

tanh(x): returns the hyperbolic tangent of a number

parameters: x : required; a number

examples


code:
                    <div>
                        <a id="math_3z"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_3z").innerHTML = Math.tanh(1);
                    </script>
                

trunc(x): returns the integer part of a number.

parameters: x : required; a number

examples


code:
                    <div>
                        <a id="math_4a"></a><br><br>
                    </div>
                    <script>
                        document.getElementById("math_4a").innerHTML = Math.trunc(8.76);
                    </script>
                

summary of math object properties

top

JavaScript provides 8 mathematical constants that can be accessed with the Math object.

E — Euler's number

LN2 — The natural logarithm of 2

LN10 — Natural logarithm of 10

LOG2E — Base 2 logarithm of E

LOG10E — Base 10 logarithm of E

PI — The number PI

SQRT1_2 — Square root of 1/2

SQRT2 — The square root of 2

examples
code:
                    <div class="spec">
                        <a id="prop"></a>     
                    </div>
                    <script>
                        document.getElementById("prop").innerHTML = 
                        "<p><b>Math.E:</b> " + Math.E + "</p>" +
                        "<p><b>Math.PI:</b> " + Math.PI + "</p>" +
                        "<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
                        "<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
                        "<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
                        "<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
                        "<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
                        "<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
                    </script>
                

numbers_math examples

top

some examples

examples

Insert number and press "enter"

code:
                    <div  class="spec">
                        <p>Insert number and press "enter" </p>
                        <input class="numberInput" type="text"> <br>
                        <a id="ex1"> </a> <br>         
                    </div>
                    <script>
                        const input = document.querySelector('.numberInput');
                        const para = document.querySelector('#ex1');
                    
                        function squared(num) {return num * num; }
                        function cubed(num) {return num * num * num;}
                        function factorial(num) {
                            let x = num;
                            while(x > 1) {
                            num *= x-1;
                            x--;
                            }
                    
                            return num;
                        }
                    
                        input.onchange = function() {
                            const num = input.value;
                            if(isNaN(num)) {
                            para.textContent = 'You need to enter a number!';
                            } else {
                            para.textContent = num + ' squared is ' +squared(num) + '. . . ' + 
                                                num + ' cubed is ' + cubed(num) + '. . . ' + 
                                                num + ' factorial is ' + factorial(num) + '.';
                            }
                        }
                    </script>
                

canvas

code:
                <div class="spec">
                    <canvas id="canvasA" width="100" height="100" style="border: 2px solid black"> <p>canvas </p> </canvas>
                    <button id="btnA" style="margin: 10px">Change color </button>     
                </div>
                <script>
                const btn = document.querySelector('#btnA');
                function random(number) {
                    return Math.floor(Math.random()*number);
                }
                function bgChange() {
                    const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';
                    document.getElementById('canvasA').style.backgroundColor = rndCol;
                }
                btn.addEventListener('click', bgChange);
                </script>
            

Picture randomizer

code:
                <div class="spec">
                    <img style="margin-left: 2vw;" name="holiday" id="holiday" src="1.jpg"  width="250" height="250"/> <br>
                    <form>
                        <input type="button" value="Click to see holiday pictures"
                        onClick="Randomizer()" />
                    </form>    
                </div>  
                <script> 
                    myImages = new Array(4);
                    for(var i=0; i <4; i++){
                    myImages[i]=new Image();
                    }
                    myImages[0].src="1.jpg";
                    myImages[1].src="2.jpg";
                    myImages[2].src="3.jpg";
                    myImages[3].src="4.jpg"; 
                    function Randomizer(){
                    var num = myImages.length - 1;
                    var ran = Math.floor(Math.random() * (num + 1));
                    document.images["holiday"].src = myImages[ran].src;
                    }
                </script>
            

Github - math examples

examples
math-abs:
                function difference(a, b) {
                    return Math.abs(a - b);
                }
                console.log(difference(3, 5));
                // expected output: 2
                console.log(difference(5, 3));
                // expected output: 2
                console.log(difference(1.23456, 7.89012));
                // expected output: 6.6555599999999995
            
math-acos:
                // Calculates angle of a right-angle triangle in radians
                function calcAngle(adjacent, hypotenuse) {
                return Math.acos(adjacent / hypotenuse);
                }
                console.log(calcAngle(8, 10));
                // expected output: 0.6435011087932843
                console.log(calcAngle(5, 3));
                // expected output: NaN
            
math-acosh:
                console.log(Math.acosh(0.999999999999));
                // expected output: NaN
                console.log(Math.acosh(1));
                // expected output: 0
                console.log(Math.acosh(2));
                // expected output: 1.3169578969248166
                console.log(Math.acosh(2.5));
                // expected output: 1.566799236972411
            
math-asin:
                // Calculates angle of a right-angle triangle in radians
                function calcAngle(opposite, hypotenuse) {
                return Math.asin(opposite / hypotenuse);
                }
                console.log(calcAngle(6, 10));
                // expected output: 0.6435011087932844
                console.log(calcAngle(5, 3));
                // expected output: NaN
            
math-asinh:
                console.log(Math.asinh(1));
                // expected output: 0.881373587019543
                console.log(Math.asinh(0));
                // expected output: 0
                console.log(Math.asinh(-1));
                // expected output: -0.881373587019543
                console.log(Math.asinh(2));
                // expected output: 1.4436354751788103
            
math-atan:
                // Calculates angle of a right-angle triangle in radians
                function calcAngle(opposite, adjacent) {
                return Math.atan(opposite / adjacent);
                }
                console.log(calcAngle(8, 10));
                // expected output: 0.6747409422235527
                console.log(calcAngle(5, 3));
                // expected output: 1.0303768265243125
            
math-atan2:
                function calcAngleDegrees(x, y) {
                    return Math.atan2(y, x) * 180 / Math.PI;
                }
                console.log(calcAngleDegrees(5, 5));
                //expected output: 45
                console.log(calcAngleDegrees(10, 10));
                //expected output: 45
                console.log(calcAngleDegrees(0, 10));
                //expected output: 90
            
math-atanh:
                console.log(Math.atanh(-1));
                // expected output: -Infinity
                console.log(Math.atanh(0));
                // expected output: 0
                console.log(Math.atanh(0.5));
                // expected output: 0.549306144334055 (approximately)
                console.log(Math.atanh(1));
                // expected output: Infinity
            
math-cbrt:
                console.log(Math.cbrt(-1));
                // expected output: -1
                console.log(Math.cbrt(1));
                // expected output: 1
                console.log(Math.cbrt(Infinity));
                // expected output: Infinity
                console.log(Math.cbrt(64));
                // expected output: 4
            
math-ceil:
                console.log(Math.ceil(.95));
                // expected output: 1
                console.log(Math.ceil(4));
                // expected output: 4
                console.log(Math.ceil(7.004));
                // expected output: 8
                console.log(Math.ceil(-7.004));
                // expected output: -7
            
math-clz32:
                // 00000000000000000000000000000001
                console.log(Math.clz32(1));
                // expected output: 31
                // 00000000000000000000000000000100
                console.log(Math.clz32(4));
                // expected output: 29
                // 00000000000000000000001111101000
                console.log(Math.clz32(1000));
                // expected output: 22
            
math-cos:
                function getCircleX(radians, radius) {
                    return Math.cos(radians) * radius;
                }
                console.log(getCircleX(1, 10));
                // expected output: 5.403023058681398
                console.log(getCircleX(2, 10));
                // expected output: -4.161468365471424
                console.log(getCircleX(Math.PI, 10));
                // expected output: -10
            
math-cosh:
                console.log(Math.cosh(0));
                // expected output: 1
                console.log(Math.cosh(1));
                // expected output: 1.543080634815244 (approximately)
                console.log(Math.cosh(-1));
                // expected output: 1.543080634815244 (approximately)
                console.log(Math.cosh(2));
                // expected output: 3.7621956910836314
            
math-e:
                function compoundOneYear(interestRate, currentVal) {
                    return currentVal * (Math.E ** interestRate);
                }
                console.log(Math.E);
                // expected output: 2.718281828459045
                console.log((1 + (1 / 1000000)) ** 1000000);
                // expected output: 2.718280469 (approximately)
                console.log(compoundOneYear(0.05, 100));
                // expected output: 105.12710963760242
            
            
math-exp:
                console.log(Math.exp(0));
                // expected output: 1
                console.log(Math.exp(1));
                // expected output: 2.718281828459 (approximately)
                console.log(Math.exp(-1));
                // expected output: 0.36787944117144233
                console.log(Math.exp(2));
                // expected output: 7.38905609893065
            
math-expm1:
                console.log(Math.expm1(0));
                // expected output: 0
                console.log(Math.expm1(1));
                // expected output: 1.718281828459045
                console.log(Math.expm1(-1));
                // expected output: -0.6321205588285577
                console.log(Math.expm1(2));
                // expected output: 6.38905609893065
            
math-floor:
                console.log(Math.floor(5.95));
                // expected output: 5
                console.log(Math.floor(5.05));
                // expected output: 5
                console.log(Math.floor(5));
                // expected output: 5
                
                console.log(Math.floor(-5.05));
                // expected output: -6
            
math-fround:
                console.log(Math.fround(5.5));
                // expected output: 5.5
                console.log(Math.fround(5.05));
                // expected output: 5.050000190734863
                console.log(Math.fround(5));
                // expected output: 5
                console.log(Math.fround(-5.05));
                // expected output: -5.050000190734863
            
math-hypot:
                console.log(Math.hypot(3, 4));
                // expected output: 5
                console.log(Math.hypot(5, 12));
                // expected output: 13
                console.log(Math.hypot(3, 4, 5));
                // expected output: 7.0710678118654755
                console.log(Math.hypot(-5));
                // expected output: 5
            
math-imul:
                console.log(Math.imul(3, 4));
                // expected output: 12
                console.log(Math.imul(-5, 12));
                // expected output: -60
                console.log(Math.imul(0xffffffff, 5));
                // expected output: -5
                console.log(Math.imul(0xfffffffe, 5));
                // expected output: -10
            
math-ln2:
                function getNatLog2() {
                    return Math.LN2;
                }
                console.log(getNatLog2());
                // expected output: 0.6931471805599453
            
math-ln10:
                function getNatLog10() {
                    return Math.LN10;
                }
                console.log(getNatLog10());
                // expected output: 2.302585092994046
            
math-log:
                function getBaseLog(x, y) {
                    return Math.log(y) / Math.log(x);
                }
                // 2 x 2 x 2 = 8
                console.log(getBaseLog(2, 8));
                // expected output: 3
                // 5 x 5 x 5 x 5 = 625
                console.log(getBaseLog(5, 625));
                // expected output: 4
            
math-log1p:
                console.log(Math.log1p(1));
                // expected output: 0.6931471805599453
                console.log(Math.log1p(0));
                // expected output: 0
                console.log(Math.log1p(-1));
                // expected output: -Infinity
                console.log(Math.log1p(-2));
                // expected output: NaN
            
math-log2:
                console.log(Math.log2(3));
                // expected output: 1.584962500721156
                console.log(Math.log2(2));
                // expected output: 1
                console.log(Math.log2(1));
                // expected output: 0
                console.log(Math.log2(0));
                // expected output: -Infinity
            
math-log2e:
                function getLog2e() {
                    return Math.LOG2E;
                }
                console.log(getLog2e());
                // expected output: 1.4426950408889634
            
math-log10:
                console.log(Math.log10(100000));
                // expected output: 5
                console.log(Math.log10(2));
                // expected output: 0.3010299956639812
                console.log(Math.log10(1));
                // expected output: 0
                console.log(Math.log10(0));
                // expected output: -Infinity
            
math-log10e:
                function getLog10e() {
                    return Math.LOG10E;
                }
                console.log(getLog10e());
            // expected output: 0.4342944819032518
            
math-max:
                console.log(Math.max(1, 3, 2));
                // expected output: 3
                console.log(Math.max(-1, -3, -2));
                // expected output: -1
                const array1 = [1, 3, 2];
                console.log(Math.max(...array1));
                // expected output: 3
            
math-min:
                console.log(Math.min(2, 3, 1));
                // expected output: 1
                console.log(Math.min(-2, -3, -1));
                // expected output: -3
                const array1 = [2, 3, 1];
                console.log(Math.min(...array1));
                // expected output: 1
            
math-pi:
                function calculateCircumference(radius) {
                    return 2 * Math.PI * radius;
                }
                console.log(Math.PI);
                // expected output: 3.141592653589793
                console.log(calculateCircumference(10));
                // expected output: 62.83185307179586
            
math-pow:
                console.log(Math.pow(7, 3));
                // expected output: 343
                console.log(Math.pow(4, 0.5));
                // expected output: 2
                console.log(Math.pow(7, -2));
                // expected output: 0.02040816326530612
                //                  (1/49)
                console.log(Math.pow(-7, 0.5));
                // expected output: NaN
            
math-random:
                function getRandomInt(max) {
                    return Math.floor(Math.random() * Math.floor(max));
                }
                console.log(getRandomInt(3));
                // expected output: 0, 1 or 2
                console.log(getRandomInt(1));
                // expected output: 0
                console.log(Math.random());
                // expected output: a number between 0 and 1
            
math-round:
                console.log(Math.round(0.9));
                // expected output: 1
                console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
                // expected output: 6 6 5
                console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
                // expected output: -5 -5 -6
            
math-sign:
                console.log(Math.sign(3));
                // expected output: 1
                console.log(Math.sign(-3));
                // expected output: -1
                console.log(Math.sign(0));
                // expected output: 0
                console.log(Math.sign('-3'));
                // expected output: -1
            
math-sin:
                function getCircleY(radians, radius) {
                    return Math.sin(radians) * radius;
                }
                console.log(getCircleY(1, 10));
                // expected output: 8.414709848078965
                console.log(getCircleY(2, 10));
                // expected output: 9.092974268256818
                console.log(getCircleY(Math.PI, 10));
                // expected output: 1.2246467991473533e-15
            
math-sinh:
                console.log(Math.sinh(0));
                // expected output: 0
                console.log(Math.sinh(1));
                // expected output: 1.1752011936438014
                console.log(Math.sinh(-1));
                // expected output: -1.1752011936438014
                console.log(Math.sinh(2));
                // expected output: 3.626860407847019
            
math-sqrt:
                function calcHypotenuse(a, b) {
                    return (Math.sqrt((a * a) + (b * b)));
                }
                console.log(calcHypotenuse(3, 4));
                // expected output: 5
                console.log(calcHypotenuse(5, 12));
                // expected output: 13
                console.log(calcHypotenuse(0, 0));
                // expected output: 0
            
math-sqrt1_2:
                function getRoot1Over2() {
                    return Math.SQRT1_2;
                }
                console.log(getRoot1Over2());
                // expected output: 0.7071067811865476
            
math-sqrt2:
                function getRoot2() {
                    return Math.SQRT2;
                }
                console.log(getRoot2());
                // expected output: 1.4142135623730951
            
math-tan:
                function getTanFromDegrees(degrees) {
                    return Math.tan(degrees * Math.PI / 180);
                }
                console.log(getTanFromDegrees(0));
                // expected output: 0
                console.log(getTanFromDegrees(45));
                // expected output: 0.9999999999999999
                console.log(getTanFromDegrees(90));
                // expected output: 16331239353195370
            
math-tanh:
                console.log(Math.tanh(-1));
                // expected output: -0.7615941559557649
                console.log(Math.tanh(0));
                // expected output: 0
                console.log(Math.tanh(Infinity));
                // expected output: 1
                console.log(Math.tanh(1));
                // expected output: 0.7615941559557649
            
math-trunc:
                console.log(Math.trunc(13.37));
                // expected output: 13
                console.log(Math.trunc(42.84));
                // expected output: 42
                console.log(Math.trunc(0.123));
                // expected output: 0
                console.log(Math.trunc(-0.123));
                // expected output: -0
            

JavaScript.info - examples

examples
ways to write a number:
                let billion = 1e9;  // 1 billion, literally: 1 and 9 zeroes
                alert( 7.3e9 );  // 7.3 billions (same as 7300000000 or 7_300_000_000)

                1e3 === 1 * 1000; // e3 means *1000
                1.23e6 === 1.23 * 1000000; // e6 means *1000000

                let mсs = 0.000001;

                let mcs = 1e-6; // five zeroes to the left from 1

                // -3 divides by 1 with 3 zeroes
                1e-3 === 1 / 1000; // 0.001
                // -6 divides by 1 with 6 zeroes
                1.23e-6 === 1.23 / 1000000; // 0.00000123
                // an example with a bigger number
                1234e-2 === 1234 / 100; // 12.34, decimal point moves 2 times
            
hex, binary and octal numbers:
                alert( 0xff ); // 255
                alert( 0xFF ); // 255 (the same, case doesn't matter)

                let a = 0b11111111; // binary form of 255
                let b = 0o377; // octal form of 255
                alert( a == b ); // true, the same number 255 at both sides
            
toString(base):
                let num = 255;
                alert( num.toString(16) );  // ff
                alert( num.toString(2) );   // 11111111
            
rounding:
                let num = 1.23456;
                alert( Math.round(num * 100) / 100 ); // 1.23456 -> 123.456 -> 123 -> 1.23

                let num = 12.34;
                alert( num.toFixed(1) ); // "12.3"

                let num = 12.36;
                alert( num.toFixed(1) ); // "12.4"

                let num = 12.34;
                alert( num.toFixed(5) ); // "12.34000", added zeroes to make exactly 5 digits
            
imprecise calculations:
                alert( 1e500 ); // Infinity

                alert( 0.1 + 0.2 == 0.3 ); // false

                alert( 0.1 + 0.2 ); // 0.30000000000000004

                alert( 0.1.toFixed(20) ); // 0.10000000000000000555

                let sum = 0.1 + 0.2;
                alert( sum.toFixed(2) ); // "0.30"

                let sum = 0.1 + 0.2;
                alert( +sum.toFixed(2) ); // 0.3

                alert( (0.1 * 10 + 0.2 * 10) / 10 ); // 0.3
                alert( (0.28 * 100 + 0.14 * 100) / 100); // 0.4200000000000001
            
parseInt and parseFloat:
                alert( +"100px" ); // NaN

                alert( parseInt('100px') ); // 100
                alert( parseFloat('12.5em') ); // 12.5
                alert( parseInt('12.3') ); // 12, only the integer part is returned
                alert( parseFloat('12.3.4') ); // 12.3, the second point stops the reading

                alert( parseInt('a123') ); // NaN, the first symbol stops the process

                alert( parseInt('0xff', 16) ); // 255
                alert( parseInt('ff', 16) ); // 255, without 0x also works
                alert( parseInt('2n9c', 36) ); // 123456
            
other math functions:
                alert( Math.random() ); // 0.1234567894322
                alert( Math.random() ); // 0.5435252343232
                alert( Math.random() ); // ... (any random numbers)
                
                alert( Math.max(3, 5, -10, 0, 1) ); // 5
                alert( Math.min(1, 2) ); // 1

                alert( Math.pow(2, 10) ); // 2 in power 10 = 1024