JavaScript - Operators

What is Javascript Operators?
  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Ternary Operators
  • Assignment Operators

    1. Arithmetic Operators :
    Arithmetic operators are used to perform arithmetic operations on the operands. The following operators are known as JavaScript arithmetic operators
        < !DOCTYPE html>
        < html>
        < body>
    
        < h1>JavaScript Arithmetic< /h1>
        < h2>Arithmetic Operations< /h2>
        < p>A typical arithmetic operation takes two numbers (or expressions) and produces a new number.< /p>
    
        < p id="demo">< /p>
    
        < script>
        let a = 3;
        let x = (100 + 50) * a;
    
        document.getElementById("demo").innerHTML = x;
    < /script>
    < /body>
    < /html>
    
    Output :
    js-op-1

    2. Comparison Operators
        Operator	        Description
        ---------   -----------------------------
        ==	         equal to
        ===	         equal value and equal type
        !=	         not equal
        !==	         not equal value or not equal type
        >	         greater than
        <	         less than
        >=	         greater than or equal to
        <=	         less than or equal to
        ?	         ternary operator
    
    Eg:
        < !DOCTYPE html>
        < html>
        < body>
        < h1>JavaScript Operators< /h1>
        < h2>The Comparison (=) Operator< /h2>
    
        < p id="demo">< /p>
    
        < script>
        // Assign the value 5 to x
        let x = 5;
        // Assign the value 2 to y
        let y = 2;
        // Assign the value x + y to z
        let z = x + y;
        // Display z
        document.getElementById("demo").innerHTML = "The sum of x + y is: " + z;
        < /script>
        < /body>
        < /html>
    
    Output :

    JavaScript Operators

    The Comparison (=) Operator

    The sum of x + y is: 7



    3. Assignment Operators :
    Assignment operators are used to perform assignment operations on the operands. The following operators are known as JavaScript assignment operators.
    Eg:
        < !DOCTYPE html>
        < html>
        < body>
    
        < h1>JavaScript String Operators< /h1>
        < h2>The += Operator< /h2>
        < p>The assignment operator += can concatenate strings.< /p>
    
        < p id="demo">< /p>
    
        < script>
        let text1 = "What a very ";
        text1 += "nice day";
        document.getElementById("demo").innerHTML = text1;
        < /script>
    
        < /body>
        < /html>
    
    Output :
    js-op-3

    4. Logical Operators
        Operator	   Description
        --------    ---------------
          &&	    logical and
          ||	    logical or
          !	        logical not
    
          Eg:
            < !DOCTYPE html>
            < html>
            < body>
            < script>
    
            let x = 2;
            // both expressions are true
            console.log((x < 4) && (4 >= x));  // true
    
            // only one expression is true
            console.log((x <= 4) && (2 == 4));  // false
    
            // both expressions are false
            console.log((x > 4) && (x == 4));  // false
            < /script>
            < /body>
            < /html>
    
    5.Ternary Operators :
    The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is false.
        Eg:
        < !DOCTYPE html>
        < html>
        < body>
    
        < h1>JavaScript Comparison< /h1>
        < h2>The () ? : Ternary Operator< /h2>
    
        < p>Input your age and click the button: < /p>
    
        < input id="age" value="18" />
        < button onclick="myFunction()">Try it < /button>
    
        < p id="demo">< /p>
    
        < script>
        function myFunction() {
        let age = document.getElementById("age").value;
        let voteable = (age < 18) ? "Too young":"Old enough";
        document.getElementById("demo").innerHTML = voteable + " to vote.";
        }
        < /script>
    
        < /body>
        < /html>
    
    
    Output :
    js-op-4
    If age is 18 and above…

    js-op-5
    If age is below 18

    js-op-6

    (JS - Objects)