JavaScript DHTML/Data Type/String Comparison
Содержание
Compare string against integer against simple test
<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>
Compare string against integer with a strict test
<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>
Comparing two strings
<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>
Identity and Equality
<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>
<p>Some page content</p>
</body>
</html>
If a lower case string is greater than, equal to, or less than the same string in upper case characters.
<HTML>
<BODY>
<H1>
<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>
</H1>
</BODY>
</HTML>