Examples of JavaScript Operators

0

All Contributions

Comparison operators

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript Comparison Operators</title>
</head>
<body>
    <script>
    var x = 25;
    var y = 35;
    var z = "25";
     
    document.write(x == z);  // Prints: true
    document.write("<br>");
    
    document.write(x === z); // Prints: false
    document.write("<br>");
    
    document.write(x != y);  // Prints: true
    document.write("<br>");
    
    document.write(x !== z); // Prints: true
    document.write("<br>");
    
    document.write(x < y);   // Prints: true
    document.write("<br>");
    
    document.write(x > y);   // Prints: false
    document.write("<br>");
    
    document.write(x <= y);  // Prints: true
    document.write("<br>");
    
    document.write(x >= y);  // Prints: false
    </script>
</body>
</html>

Output :

true

false

true

true

true

false

true 

false

Logical operators

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript Logical Operators</title>
</head>
<body>
    <script>
    var year = 2018;
 
    // Leap years are divisible by 400 or by 4 but not 100
    if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))){
        document.write(year + " is a leap year.");
    } else{
        document.write(year + " is not a leap year.");
    }
    </script>
</body>
</html>

Output :

2018 is not a leap year 

Increment decrement operators

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript Incrementing and Decrementing Operators</title>
</head>
<body>
    <script>
    var x; // Declaring Variable
 
    x = 10;
    document.write(++x); // Prints: 11
    document.write("<p>" + x + "</p>");   // Prints: 11
     
    x = 10;
    document.write(x++); // Prints: 10
    document.write("<p>" + x + "</p>");   // Prints: 11
     
    x = 10;
    document.write(--x); // Prints: 9
    document.write("<p>" + x + "</p>");   // Prints: 9
     
    x = 10;
    document.write(x--); // Prints: 10
    document.write("<p>" + x + "</p>");   // Prints: 9
    </script>
</body>
</html>

Output :

11

11

 

10

11

 

9

 9

 

10

9

String operators

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript String Operators</title>
</head>
<body>
    <script>
    var str1 = "Hello";
    var str2 = " World!";
    
    document.write(str1 + str2 + "<br>"); // Outputs: Hello World!
     
    str1 += str2;
    document.write(str1); // Outputs: Hello World!
    </script>
</body>
</html>

Output :

Hello World!

Hello World! 

Assignment operators

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JavaScript Assignment Operators</title>
</head>
<body>
    <script>
    var x;    // Declaring Variable
 
    x = 10;
    document.write(x + "<br>"); // Prints: 10
    
    x = 20;
    x += 30;
    document.write(x + "<br>"); // Prints: 50
     
    x = 50;
    x -= 20;
    document.write(x + "<br>"); // Prints: 30
     
    x = 5;
    x *= 25;
    document.write(x + "<br>"); // Prints: 125
     
    x = 50;
    x /= 10;
    document.write(x + "<br>"); // Prints: 5
     
    x = 100;
    x %= 15;
    document.write(x); // Prints: 10
    </script>
</body>
</html>

Output :

10

50

30

125

 5

10

total contributions (6)

This topic belongs to these lists

need a help?


find thousands of online teachers now

New to examplegeek.com?

Join us