close
The Wayback Machine - https://web.archive.org/web/20211206092037/https://www.geeksforgeeks.org/javascript-operators/amp/

JavaScript Operators

Below is the example of the basic comparison operators. 
 

Example 1:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




<script>
    function gfg() {
    let val1 = 5;
    // Equality Operators
    document.write(val1 == 5);
    document.write("<br>");
    // Relational Operators
    document.write(val1 > 0);
    }
    gfg();
</script>

Output:

true
true

An operator is capable of manipulating a certain value or operand. Operators are used to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript operators are used for compare values, perform arithmetic operations etc. There are various operators supported by JavaScript:  



Arithmetic Operators: 
There are various Arithmetic Operators – 

Y = 5 + 5 gives Y = 10
Y = "Geeks" + "for" + "Geeks" gives Y = "GeeksforGeeks"
Y = "Geeks" + 4 + "Geeks" gives Y = "Geeks4Geeks"
Y = 5 - 3 gives Y = 2 
Y = 5 * 5 gives Y = 25
Y = 5 / 5 gives Y = 1
A % B means remainder (A/B)
     Y = 5 % 4 gives Y = 1
let A = 10 and Y = A + + then A = 11, Y=10
 if  A = 10 and Y = + + A then A = 11, Y=11
let A = 10 and Y = A - - then A = 9, Y=10
 if  A = 10 and Y = - - A then A = 9, Y=9

Assignment Operators: 
There are various Assignment Operators in JavaScript – 

 If A = 10 and Y = A then Y = 10
Y += 1 gives Y = Y + 1 
Y -= 1 gives Y = Y - 1 
Y *= A is equivalent to Y = Y * A
Y /= A is equivalent to Y = Y / A
Y %= A is equivalent to Y = Y % A

Comparison Operators: 
There are various Comparison Operators in JavaScript – 

Y = 5 and X = 6
  Y = = X is false.             
             
given X = 10 then X = = = "10" is false.
X = = = 10 is true.
given X = 10 then X ! = 11 is true. 
given X = 10 then X > 11 is false. 
given X = 10 then X < 11 is true. 
given X = 10 then X > = 11 is false. 
given X = 10 then X < = 10 is true. 

Logical Operators: 
There are various Logical Operators in JavaScript – 

Y = 5 and X = 6
Y && X is true.
Y = 5 and X = 0
Y || X is true.
Y = 5 and X = 0
!(Y || X) is false.

Ternary Operator: 

Y =  ? A : B 
Y = (6>5) ? 6 : 5
therefore Y = 6
typeof variable;

Example: 




<html>
<head>
<title> GfG typeof example </title>
</head>
<body>
     
    <script type="text/javascript">
            var a = 17;
            var b = "GeeksforGeeks";
            var c = "";
            var d = null;
 
            document.write("Type of a = " + (typeof a));
            document.write("<br>");
 
            document.write("Type of b = " + (typeof b));
            document.write("<br>");
 
            document.write("Type of c = " + (typeof c));
            document.write("<br>");
 
            document.write("Type of d = " + (typeof d));
            document.write("<br>");
 
            document.write("Type of e = " + (typeof e));
            document.write("<br>");
    </script>
</body>
</html>                   

Output: 

Type of a = number
Type of b = string
Type of c = string
Type of d = object
Type of e = undefined

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.




Article Tags :