Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - OPERATORS


Operators are used in Python to perform calculations or operations.


Say for example, we have used the addition + operator to add two numbers.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
   		
   	var x = 5 
	var y = 6
	var z = x + y
	document.write("The added value is : ",z)  
		
</script>      
</body>
</html>


Output :



  The added value is : 11

In the above example, we have used two operators of JavaScript.

  1. Addition Operator (i.e. '+')

    Used to add two or more values.

    var z = x + y

  2. Assignment Operator (i.e. '=')

    Used to assign a value to a variable.

    var x = 5
    var y = 6
    var z = x + y

So far, we have seen only two operators. Now, let us see all the operators available in JavaScript.


Array of Operators available in JavaScript

  1. Arithmetic Operators



    Arithmetic Operators are used to perform Arithmetic operations like addition, subtraction, multiplication e.t.c.

    Let us see them below.



    Operator Used For Example
    + Addition x + y
    - Subtraction x - y
    * Multiplication x * y
    / Division x / y
    % Modulus x % y
    ++ Increment x++
    -- Decrement x--
    ** Exponentiation x**y


    CLICK HERE TO SEE ARITHMETIC OPERATORS IN DETAIL



    1. Addition Operator (+)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var y = 6
      	var z = x + y
      	document.write("The added value is : ",z)
      	
      </script>
      </body>
      </html>
      


      Output :



        The added value is : 11

    2. Subtraction Operator (-)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 6
      	var z = x + y
      	document.write("The subtracted value is :",z)
      	
      </script>
      </body>
      </html>
      


      Output :



        The subtracted value is : 6

    3. Multiplication Operator (*)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var x = 5
      	var y = 6
      	var z = x * y
      	document.write("The multiplied value is :",z)
      	
      </script>
      </body>
      </html>
      


      Output :



        The multiplied value is : 30

    4. Division Operator (/)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var x = 8
      	var y = 2
      	var z = x / y
      	document.write("The divided value is :",z)
      
      </script>
      </body>
      </html>
      


      Output :



        The divided value is : 4

    5. Modulus Operator (%)



      Modulus Operator calculates the remainder.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 9
      	var y = 2
      	var z = x % y
      	document.write("The modulus is :",z)
      	
      </script>
      </body>
      </html>
      


      Output :



        The modulus is : 1


      i.e. The remainder of 9 and 2 is 1.

    6. Increment Operator (++)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var x = 2
      	x++
      	document.write("The incremented value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The incremented value is : 3


      The increment operator ++ with x,

      		x++


      Actually means, increment the value of x by 1.

      In other words,

      		x++


      Is,

      		x = x + 1


      So, the value of x is 2. And it gets incremented by 1.

      And the current value of x is 3.

    7. Decrement Operator (--)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var x = 2
      	x--
      	document.write("The decremented value is :",x)
      
      </script>
      </body>
      </html>
      


      Output :



        The decremented value is : 1


      The decrement operator -- with x,

      		x--


      Actually means, decrement the value of x by 1.

      In other words,

      		x--


      Is,

      		x = x - 1


      So, the value of x is 2. And it gets incremented by 1.

      And the current value of x is 1.

    8. Exponentiation Operator (**)



      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      		var x = 2
      	var y = 3
      	var z = x ** y
      	document.write("The exponential is : ",z)
      
      </script>
      </body>
      </html>
      


      Output :



        The exponential value is : 8


      In the above code we are calculating (2)3 .

      i.e. 2*2*2 = 8



  2. Bitwise Operators



    There are a few number system. Among which we usually use the decimal numbers (i.e. 1, 2, 3, .... ). And there is also something called Binary Numbers(i.e. 0 and 1).

    Say for example, if we convert the decimal number 5 to Binary, it is 101.

    0 and 1 are also called as Bit. And the operator that we are going to use, deals with Bits.

    So, they are called as Bitwise Operators.

    Let us see them below :



    Operator Name Example
    & AND x & y
    | OR x | y
    ~ NOT ~x
    ^ XOR x ^ y
    >> Right Shift x >>
    << Left Shift x <<
    >>> Zero fill right Shift x >>>


    CLICK HERE TO SEE BITWISE OPERATORS IN DETAIL



    1. The Operator & or AND



      The & Operator checks, if both the bits are 1 then it returns 1 else it returns 0.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 2
      	var z = x & y
      	document.write(z)
      
      </script>
      </body>
      </html>
      


      Output :



        0


      So, in the above code, we have,

      		x = 5


      And,

      		y = 2


      So, what JavaScript does internally is, converts both the numbers (i.e. 5 and 2) to binary.

      The binary equivalent of 5 is 101

      And the binary equivalent of 2 is 10

      Now, if we calculate the & of 5 and 2.

      			101
      		&   010
      			----
      			000


      And we got the output 000 because we could only get a 1 if both are 1.

      But in this case, 1 & 0 is 0, 0 & 1 is 0 and 1 & 0 is also 0 i.e. 000.

      And if we convert 000 to decimal we get 0 as output.

    2. The Operator | or OR



      The | Operator checks, if either one of the bits is 1 then it returns 1 else it returns 0.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 2
      	var z = x | y
      	document.write(z)
      	
      </script>
      </body>
      </html>
      


      Output :



        7


      So, in the above code, we have,

      		x = 5


      And,

      		y = 2


      So, what JavaScript does internally is, converts both the numbers (i.e. 5 and 2) to binary.

      The binary equivalent of 5 is 101

      And the binary equivalent of 2 is 10

      Now, if we calculate the | of 5 and 2.

      			101
      		|   010
      			----
      			111


      And we got the output 111 because we can get a 1 if either of the bits are 1.

      But in this case, 1 | 0 is 1, 0 | 1 is 1 and 1 | 0 is also 1 i.e. 111.

      And if we convert 111 to decimal, it is 7.

    3. The Operator ~ or '' NOT



      The ~ Operator or not, simply converts 1s to 0s and 0s to 1s.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var z = ~x
      	document.write(z)
      	
      </script>
      </body>
      </html>
      


      Output :



        -6


      If we look at the output, it looks a little weird. Where on earth did -6 come from.

      Well! Let us not get much deeper into it and understand a simple formula.

      i.e.

      ~x can be written as -x-1 or -(x+1)

      So, if we apply the same formula to the above code, we have,

      		x = 5


      And, the binary equivalent of 5 is 101

      Now, if as per the formula,

      	~x = -(101 + 1)
      
      101 1 ---- 110


      The decimal equivalent of 110 is 6.

      So,

      		~x = -6


      And thus we get the output as -6.

    4. The Operator ^ or XOR



      The ^ or XOR/eXclusive OR is almost same as the OR Operator. The only difference is, if both are 1 it returns 0. Else for 1 and 0 or 0 and 1 it returns 1.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var y = 4
      	var z = x ^ y
      	document.write(z)
      	
      </script>
      </body>
      </html>
      


      Output :



        1


      So, in the above code, we have,

      			x = 5


      And,

      			y = 4


      So, what JavaScript does internally is, converts both the numbers (i.e. 5 and 2) to binary.

      The binary equivalent of 5 is 101

      And the binary equivalent of 2 is 100

      Now, if we calculate the | of 5 and 2.

      				101
      			^   100
      				----
      				001


      And we got the output 001 because 1 ^ 1 is 0, 0 ^ 0 is 0 and 1 ^ 0 is 1

      i.e. 001.

      And if we convert 001 to decimal, it is 1.

      Just note that 1 ^ 1 is 0 and not 1.

    5. The Operator >> or Right Shift



      The >> Operator or Right Shift, simply shifts bits to the right by number of positions specified.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var z = x >> 1
      	document.write(z)
      	
      </script>
      </body>
      </html>
      


      Output :



        2


      So, in the above code, we have,

      		x = 5


      And we try Right Shifting the value of x by 1.

      		z = x >> 1


      So, what JavaScript does internally is, converts the number (i.e. 5) to binary.

      The binary equivalent of 5 is 101

      Now, if we right shift 101 by 1.

      		101 >> 010


      And after Right Shifting by 1, the 1 to the extreme right is lost.

      And we got the output 010.

      And if we convert 010 to decimal, it is 2.

    6. The Operator << or Left Shift



      The >> Operator or Left Shift, simply shifts bits to the left by number of positions specified.

      Let us understand it with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var z = x << 2
      	document.write(z)
      	
      </script>
      </body>
      </html>
      


      Output :



        20


      So, in the above code, we have,

      		x = 5


      And we try Left Shifting the value of x by 2.

      		z = x << 2


      So, what JavaScript does internally is, converts the number (i.e. 5) to binary.

      The binary equivalent of 5 is 101

      Now, if we left shift 101 by 2.

      		'00101' >> '10100'


      And after Left Shifting by 2, all the elements shifts 2 places to the right.

      And we got the output 10100.

      And if we convert 10100 to decimal, it is 20.

  3. Assignment Operators



    We have already used the Assignment Operator i.e. =. When we tried to initialise any variable.

    Say for example,

    Example :



    <html>
    <body>
    <script language = "javascript" type = "text/javascript">
    	
    	var x = 5
    	var y = 6
    	var z = x + y
    	document.write(z)
    	
    </script>
    </body>
    </html>
    


    Although, there is just one assignment operator i.e. =. But there are various ways by which we can write it.

    Let us see them below.



    Operator Example Is Same As
    = x = 1 x = 1
    += x += 1 x = x + 1
    -= x -= 1 x = x - 1
    *= x *= 1 x = x * 1
    /= x /= 1 x = x / 1
    %= x %= 1 x = x % 1
    **= x **= 1 x = x ** 1
    //= x //= 1 x = x // 1
    &= x &= 1 x = x & 1
    |= x |= 1 x = x | 1
    ^= x ^= 1 x = x ^ 1
    >>= x >>= 1 x = x >> 1
    <<= x <<= 1 x = x << 1


    CLICK HERE TO SEE ASSIGNMENT OPERATORS IN DETAIL



    1. The Operator =



      The = operator is called the assignment operator. It says that the value at the right hand side should be assigned to the variable at the left hand side.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	document.write("The value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The value of x is : 5

    2. The operator +=



      Say, we have a statement,

      	x = x + 3


      Now, we can shorten the above statement by using the operator +=.

      	x += 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x += 3
      	document.write("The value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 8

    3. The operator -=



      Say, we have a statement,

      	x = x - 3


      Now, we can shorten the above statement by using the operator -=.

      	x -= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x -= 3
      	document.write("The new value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 2

    4. The operator *=



      Say, we have a statement,

      	x = x * 3


      Now, we can shorten the above statement by using the operator *=.

      	x *= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x *= 3
      	document.write("The new value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 15

    5. The operator /=



      Say, we have a statement,

      	x = x / 3


      Now, we can shorten the above statement by using the operator /=.

      	x /= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 12
      	x += 3
      	document.write("The new value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 4

    6. The operator %=



      Say, we have a statement,

      	x = x % 3


      Now, we can shorten the above statement by using the operator %=.

      	x %= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x %= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 2

    7. The operator **=



      Say, we have a statement,

      	x = x ** 3


      Now, we can shorten the above statement by using the operator **=.

      	x **= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	x **= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 125

    8. The operator //=



      Say, we have a statement,

      	x = x // 3


      Now, we can shorten the above statement by using the operator //=.

      	x //= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x //= 3
      	document.write("The value of x is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 1

    9. The operator &=



      Say, we have a statement,

      	x = x & 3


      Now, we can shorten the above statement by using the operator &=.

      	x &= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 12
      	x &= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 1

    10. The operator |=



      Say, we have a statement,

      	x = x | 3


      Now, we can shorten the above statement by using the operator |=.

      	x |= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x |= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 7

    11. The operator ^=



      Say, we have a statement,

      	x = x ^ 3


      Now, we can shorten the above statement by using the operator ^=.

      	x ^= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x ^= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 6

    12. The operator >>=



      Say, we have a statement,

      	x = x >> 3


      Now, we can shorten the above statement by using the operator >>=.

      	x >>= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	x >>= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 0

    13. The operator <<=



      Say, we have a statement,

      	x = x << 3


      Now, we can shorten the above statement by using the operator <<=.

      	x <<= 3


      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 12
      	x <<= 3
      	document.write("The new value is :",x)
      	
      </script>
      </body>
      </html>
      


      Output :



        The new value of x is : 40

  4. Comparison Operators



    There would be scenarios, where we would have to compare two values.

    Say, we wanted to check, if the first number is equal to the other number or not. Or if one number is greater than or less than the other number and so on.

    Usually, we would be using the Comparison Operators with if statements and Loops. Which we would be learning in later tutorials.

    For now we can compare the values and just document.write the results. The result would be true if the condition matches. Else it would return false.

    There are various comparison operators we have listed below.



    Operator Name Example
    == Equal To x == y
    === Equal To for Value and Type x === y
    != Not Equal x != y
    !== Not Equal To for Value and Type x !== y
    > Greater Than x > y
    < Less Than x < y
    >= Greater Than or Equal To x >= y
    <= Greater Than or Equal To x <= y


    CLICK HERE TO SEE COMPARISON OPERATORS IN DETAIL



    1. The Operator ==



      It is used to compare two values and check if they are equal or not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var y = 7
      	document.write(x == y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Since, the values of x and y are not equal. We got the output as false.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var y = 5
      	document.write(x == y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Now, the values of x and y are equal. We got the output as true.

    2. The Operator ===



      It is used to compare two values and check if they are equal or not and are also of the same data type.

      Say, for example, we have an integer value that is 6.

      		var x = 6


      Also we have a String that is "6".

      		var y = "6"


      Now, let us try comparing them using the == operator.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 6
      	var y = "6"
      	document.write(x == y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      So, in the above code, although the variable x holds an integer,

      		var x = 6


      And the variable y holds a String.

      		var y = "6"


      The operator == returns true.

      		document.write(x == y)


      And this is where === comes handy. It not only checks the value but also checks if both the values belongs to the same data type.

      So, if we rewrite the above example using === operator.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 6
      	var y = "6"
      	document.write(x === y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      And this time we got the output as false, because === operator.

      		document.write(x === y)


      Not only checks the value but also checks if both the values belongs to the same data type.

      And this time the values are of different data type.

      The variable x is of integer type.

      		var x = 6


      And the variable y is of String type.

      		var y = "6"

    3. The Operator !=



      It is used to compare two values and check if they are not equal. ! is a sign for not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      		
      	var x = 5
      	var y = 7
      	document.write(x != y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Since, the values of x and y are not equal. We got the output as true.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 5
      	document.write(x != y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Now, the values of x and y are equal. We got the output as false.

    4. The Operator !==



      It is used to compare two values and check if they are equal or not and they are not of the same data type.

      Say, for example, we have an integer value that is 6.

      		var x = 6


      Also we have a String that is "6".

      		var y = "6"


      Now, let us try comparing them using the == operator.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 6
      	var y = "6"
      	document.write(x != y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      So, in the above code, although the variable x holds an integer,

      		var x = 6


      And the variable y holds a String.

      		var y = "6"


      The operator != returns false.

      		document.write(x != y)


      And this is where !== comes handy. It not only checks the value but also checks if both the values should not belong to the same data type.

      So, if we rewrite the above example using !== operator.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 6
      	var y = "6"
      	document.write(x !== y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      And this time we got the output as true, because !== operator.

      		document.write(x !== y)


      Not only checks the value but also checks if both the values does not belong to the same data type.

      And this time the values are of different data type.

      The variable x is of integer type.

      		var x = 6


      And the variable y is of String type.

      		var y = "6"

    5. The Operator >



      It is used to compare two values and check if the first value is greater than the second or not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 7
      	document.write(x > y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Since, the values of x is not greater than y. We got the output as false.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 7
      	var y = 5
      	document.write(x < y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Now, the values of x is greater than y. We got the output as true.

    6. The Operator <



      It is used to compare two values and check if the first value is less than the second or not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 7
      	document.write(x < y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Since, the values of x is less than y. We got the output as true.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 7
      	var y = 5
      	document.write(x < y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Now, the values of x is not less than y. We got the output as false.

    7. The Operator >=



      It is used to compare two values and check if the first value is greater or equal to the second or not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 7
      	document.write(x >= y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Since, the values of x is not greater than or equal y. We got the output as false.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 5
      	document.write(x >= y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Now, the values of x is equal than y. We got the output as true.

    8. The Operator <=



      It is used to compare two values and check if the first value is less than or equal to the second or not.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 5
      	var y = 7
      	document.write(x <= y)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      Since, the values of x is less than y. We got the output as true.

      Let us see the next Example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      	
      	var x = 7
      	var y = 5
      	document.write(x <= y)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      Now, the values of x is not less than y. We got the output as false.

  5. Logical Operators



    There can be scenarios where we need to compare more than two values.

    Say for example,

    There are four numbers and we want to check if the first number is greater than the second. And also need to check if the third number is greater than the fourth number.

    Something like the below one,

    firstNumber > secondNumber and thirdNumber > fourth_number


    And if you see the above statement, we need an and statement to compare four values.

    And thus, Logical Operators comes into picture. It can be and, or, not.

    Let's see them below :



    Operator Description Example
    && It is true when both conditions are true. i < j && k < l
    || It is true when one of the condition is true i < j || k < l
    ! As the name suggests, it calculates the inverse !(i < j || k < l)


    CLICK HERE TO SEE LOGICAL OPERATORS IN DETAIL



    1. The Operator &&



      It is true when both conditions are true.

      Let us clear with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var i = 5
      	var j = 7
      	var k = 8
      	var l = 9
      	document.write(i < j && k < l)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      The above statement checks if i < j and k < l.

      Now, since 5 is less than 7 and also 8 is less than 9. The document.write statement returns true.

      		document.write(i < j && k < l)


      Let us see another example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var i = 5
      	var j = 7
      	var k = 8
      	var l = 4
      	document.write(i < j && k < l)
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      So, the above statement checks if i < j and k < l.

      Now, since 5 is less than 7 but 8 is not less than 4. The document.write statement returns false.

      	document.write(i < j && k < l)


      Just remember, for && operator, both the conditions needs to be satisfied.

    2. The Operator ||



      It is true when one of the condition is true.

      Let us clear with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var i = 5
      	var j = 7
      	var k = 8
      	var l = 4
      	document.write(i < j || k < l)
      	
      </script>
      </body>
      </html>
      


      Output :



        true


      So, the above statement checks if i < j or k < l.

      Now, since 5 is less than 7 and although 8 is not less than 4. The document.write statement returns true.

      	document.write(i < j || k < l)


      Just remember, for or operator, if only one condition works then the entire condition becomes true.

    3. The Operator !



      It is true when a condition is false. As the name suggests, it calculates the inverse.

      Let us clear with the below example.

      Example :



      <html>
      <body>
      <script language = "javascript" type = "text/javascript">
      
      	var i = 5
      	var j = 7
      	var k = 8
      	var l = 4
      	document.write(!(i < j || k < l))
      	
      </script>
      </body>
      </html>
      


      Output :



        false


      So, the above statement checks if i < j or k < l.

      Now, since 5 is less than 7 and although 8 is not less than 4. The condition is true. And since we are using not operator, false is printed.

      	document.write(!(i < j || k < l))