Example

Aritmetic Operators

HTML

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Aritmetic Operators</title>
</head>  
<body>
<!--
    Different types of JavaScript operators:
        - Aritmetic Operators
        - Assignment Operators
        - Comparison Operators
        - Logical Operators
        - Conditional Operators
    Aritmetic Operators : are used to perform arithmetic on numbers
        +	Addition
        -	Subtraction
        *	Multiplication
        **	Exponentiation  
        /	Division
        %	Modulus (Division Remainder)
        ++	Increment
        --	Decrement
    Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).  
    Adding Strings, will concatenate strings.
    Adding Strings and Numbers, will return a string.
-->

    <h1>Demo: Aritmetic Operators</h1>

    <!-- JavaScript code Starts here -->
    <script>

        /************************************************/
        // JavaScript Aritmetic Operators 
        // Example on Numbers datatype
        //
        let x = 6;
        let y = 2;
        let a = x + y;   // Addition = 8
        let s = x - y;   // Subtraction = 4
        let z = x * y;   // Multiplication = 12
        let p = z/2;     // Division = 6
        let q = x**y;    // Exponentiation = 36
        let r = q%y;     // Modulus = 0
        let i = ++x;     // Increment = 7
        let d = --y;     // Decrement = 1
        let PEMDAS = x+y*x/y-x+z; // PEMDAS = P- Parentheses, E- Exponents, M- Multiplication, D- Division, A- Addition, and S- Subtraction
        document.write( " a = " + a + "<BR>" +
                        " s = " + s + "<BR>" +
                        " z = " + z + "<BR>" +
                        " p = " + p + "<BR>" +
                        " q = " + q + "<BR>" +
                        " r = " + r + "<BR>" +
                        " i = " + i + "<BR>" +
                        " d = " + d + "<BR>" +
                        " x = " + x + "<BR>" +
                        " y = " + y + "<BR>" +
                        " PEMDAS = " +PEMDAS + "<BR><BR>")


        /************************************************/
        // JavaScript Aritmetic Operator(+) 
        // Example on Strings datatype
        //
        let text1 = "SKILL";
        let text2 = "ZAM";
        let text3 = text1 + text2; // + operator used to add (concatenate) strings
        document.write("Concatination of two strings : " +
                        text3 + 
                        "<BR><BR>")                


        /************************************************/
        // JavaScript Aritmetic Operator(+)  
        // Example on Numbers & Strings datatype
        //
        let value1 = 4;
        let value2 = "96";
        let value3 = value1 + value2;    // Concatination of Number & string
        document.write("Concatination of Number & string : " + 
                        value3 + 
                        " and the typeof is " + 
                        typeof(value3) + 
                        "<BR><BR>")    

    </script>
    <!-- JavaScript code Ends here -->

</body>
</html>

Output

View original