JavaScript DHTML/Data Type/String Comparison

Материал из Web эксперт
Перейти к: навигация, поиск

Compare string against integer against simple test

   <source lang="html4strict">
 

<html> <head>

   <title>Equality</title>
   <script type = "text/javascript">
   var x = 2;
   var y = "2";
   if (x == y) {
       document.write("x is equal to y with a simple test.");
   } else {
       document.write("x is not equal to y");
   }
   </script>

</head> </html>


 </source>
   
  


Compare string against integer with a strict test

   <source lang="html4strict">
 

<html> <head>

   <title>Equality</title>
   <script type = "text/javascript">
   var x = 42;
   var y = "42";
   if (x ===y) {
       document.write("x is equal to y with a strict test.");
   } else {
       document.write("x is not equal to y");
   }
   </script>

</head> <body> </body> </html>


 </source>
   
  


Comparing two strings

   <source lang="html4strict">
  

<html>

 <head>
   <title>Comparing two strings</title>
   <script type="text/javascript">
       var a = "C";
       var b = "J";
       
       if (a == b)
       {
           var identity = "same";
       }
       else
       {
           var identity = "different";
       }
       
       document.write(a + " and " + b + ": " + identity);    
   </script>
 </head>
 <body>
 </body>

</html>


 </source>
   
  


Identity and Equality

   <source lang="html4strict">
  

<html> <head> <title>Identity and Equality</title> <script type="text/javascript"> var sValue = "3.0"; var nValue = 3.0; if (nValue == "3.0") document.write("According to equality, value is 3.0"); if (nValue ==="3.0") document.write("According to identity, value is 3.0"); if (sValue != 3.0) document.write("According to equality, value is not 3.0"); if (sValue !== 3.0) document.write("According to identity, value is not 3.0"); </script> </head> <body>

Some page content

</body> </html>


 </source>
   
  


If a lower case string is greater than, equal to, or less than the same string in upper case characters.

   <source lang="html4strict">
 
  

<HTML> <BODY>

<SCRIPT> if ("aaa" > "AAA"){ document.write("Lower case is greater than upper case!"); } else if ("aaa" == "AAA"){ document.write("They are the same!"); } else { document.write("Upper case is greater than lower case!"); } </SCRIPT>

</BODY> </HTML>


 </source>