javaScript comparison operators

the javascript comparison operators are used to compare two values.it can be used in logical statements.

the result of a comparison operation is always true or false.

given that x = 4, the table below explains the comparison operators:

operatordescriptionexamplereturnstry   it
<less thanx < 4false 
>greater thanx > 4false 
<=less than or equal tox <= 4true 
>=greater than or equal tox >=4 true 
==equal to x == 4 true 
===equal to (strict equality)x === 4true 
!=not equal x != 5true 
!==not equal (strict inequality)x !== 4false

note :- (===) and (!==) operators behave the same as the equality operators,but the both operands type must match.a strict comparison (===,!==) is only true if the operands are the same type.

comparing strings :- when you compare strings, javascript evaluates the comparison based on strings lexicographic order. lexicographic order is essentially alphabetic order, with a few extra rules thrown in to deal with upper-case and lower-case characters as well as to accommodate strings of different lengths.

the following general rules apply:

  • lowercase characters are less than uppercase characters.
  • shorter strings are less than longer strings.
  • letters occurring earlier in the alphabet are less than those occurring later.
  • characters with lower ascii or unicode are less than those with larger values.

javaScript logical operators

javaScript logical operators are typically used with boolean values,when they are, they return a boolean value.

the conditional operators (&& and ||) can only be applied to boolean operands.

the most common use of the logical operators is to control the flow of script execution using with control flow statements.

operatordescriptionexamplereturnstry   it
&&logical and operatorif(20>=10&&10==10)true 
||logical or operatorif(4!==4 || 5<6)true 
!logical not operator!(12==12)false 
<script>

           var   x  = 4;
                      
           var   y  = 5;
                      
                          
                 if(++x==y&&++x==y){
                          
                                                 document.write("X__value_____"+x);
                                                     
                                                 document.write("y__value_____"+y);
                                      
                                             }else{
                                                          
                                                 document.write("x_____"+x);
                                                          
                                                 document.write("<br/>"+"y_____"+y);                                               
                                               }
</script>                                       

output:  x___value___6       y___value___5


javaScript comma operator

the comma(,) operator allows multiple expressions to be evaluated in a single statement and returns the result of the last expression.

              var  a =30 , b =50, c = "ram" ;   //declaration and initialization
                      
                      var  x, y, z;
                        
                        x = (y=50, z=60);
                        
                        document.write(x+"____"+y+"_____"+z);

commas are also used to separate parameters in function calls.


JavaScript typeof operator

the javascript typeof operator returns a string indicating the data type of its operand.look our below example :-

   <script>
   
var  first_value = 2000; var second_value =33300.333; var third_value=3434e3;
                  
var  first_string="javascript type of operator"; var second_string = "what is typeof";
                  
document.write("<br/>"+"type of first_value is________"+typeof(first_value)+
                                 
                      "<br/>"+"type of second_value is________"+typeof(second_value)+
                                 
                      "<br/>"+"type of third_value is________"+typeof(third_value)+
                                 
                      "<br/>"+"type of first_string is________"+typeof(first_string)+
                                 
                      "<br/>"+"type of second_string is________"+typeof(second_string)+
                                 
                   "<br/>"+"type of third_string is________"+typeof(third_string));  
</script>

javaScript conditional operator

the javaScript conditional operator returns one of two values based on the logical value of the condition.

the conditional operator  ? :  also called the 'ternary' operator.

syntax:

         var  value = (condition) ?  state__1 : state__2 ;

in the above statements, if condition is true, the state__1 is execute otherwise the state__2 is executed.

   <script>
          
          function  output(){   
               
                      var  a = (20 >= 19) ? 20 : 19;
               
                                document.write("the value of a ______"+a);
               
    (5 > 4 && 4 == 4) ? alert("this is true part") : alert("this is the false part");
               
                      }  
       
   </script>