type="text/javascript"> var value1 = , value2= ; document.write("<br>Data1 : "+value1); document.write("<br>Data2 : "+value2); document.write("<br><br>Whether Data1 = Data2 : "+(value1==value2)); document.write("<br>Whether Data1 < Data2 : "+(value1<value2)); document.write("<br>Whether Data1 > Data2 : "+(value1>value2)); document.write("<br>Whether Data1 <= Data2 : "+(value1<=value2)); document.write("<br>Whether Data1 >= Data2 : "+(value1>=value2)); document.write("<br>Whether Data1 != Data2 : "+(value1!=value2)); </script> </Body> </Html> Illustration . Using Relational Operators Output: . . Logical Operators: Logical operators perform logical (boolean) operations.
Logical operators combine or invert boolean values. Once comparisons are made, the logical operators && (AND), || (OR) and ! (NOT) can be used to create more complex conditions. Table: .
Logical or Boolean operators Logical Operator Example Meaning Result && (( > ( && ) < )) ((expr1) && (expr2)) (Logical AND) Returns true if expr1 and expr2 both true. True || (( > ( || ) < )) ((expr1) || (expr2)) (Logical OR) Returns true if either expr1 or expr2 is true, or both are true. True ! !( < ) !(expr1) (Logical NOT) Returns true if expr1 is false; otherwise, returns false.
False Chapter Pages Usage : Best practice is to use logical operators on boolean operands. However, operands of any type can be combined. The strict rules are as follows: • For && (AND) the result is false if the first operand is false; otherwise, the result is the Boolean value of the second operand. • For || (OR) the result is true if the first operand is true; otherwise, the result is the Boolean value of the second operand.
• For ! (NOT) the result is true if the operand is false; otherwise, the result is true. <Html> <Head> <Title>Demo Program - To test Logical Operators in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var value1 = , value2= ; document.write("<br>Data1 : "+value1); document.write("<br>Data2 : "+value2); var res1=((value1> ) && (value1> )); var res2=((value1> ) || (value1> )); var res3=(!(value1!=value2)); document.write("<br><br>Whether Data1> AND Data1> : "+res1); document.write("<br><br>Whether Data1> OR Data1> : "+res2); document.write("<br>Whether !Data1 != Data2 : "+res3); </script> </Body> </Html> Illustration . Using Logical Operators Output: Chapter Pages .
. String Operators: One of the built-in features of JavaScript is