First of all, I apologize for the late update of the column.
I'm not talking much about the excuses ......
String comparison
= |
Equal |
! = |
Not equal |
=== |
Absolutely equal |
! = |
Not absolutely equal |
> |
Greater |
> = |
Greater than or equal |
< |
Less |
<= |
Less than or equal |
Equal or not
VaR str1 = "JavaScript"; var str2 = "JavaScript"; document. write ("str1 =" + str1); document. write ("<br/>"); document. write ("str2 =" + str2); document. write ("<br/>"); If (str1 = str2) {document. write ("str1 and str2 are equal! ");} Else {document. Write (" str1 and str2 are not equal! ");}
Result:
Absolutely equal
"=" And "! = "Not only the data content, but also the data type.
VaR str1 = "JavaScript"; var str2 = new string ("JavaScript"); document. write ("str1 content:" + str1 + ", str1 data type:" + typeof (str1); document. write ("<br/>"); document. write ("str2 content:" + str2 + ", str2 data type:" + typeof (str2); document. write ("<br/>"); If (str1 = str2) {document. write ("str1 and str2 are absolutely equal! ");} Else {document. Write (" str1 and str2 are not absolutely equal! ");}
Result:
Greater than or less
VaR str1 = "ABC"; var str2 = "ABC"; var str3 = "BCD"; if (str1> str2) {document. write ("str1 greater than str2");} else {document. write ("str1 less than str2");} document. write ("<br/>"); If (str2> str3) {document. write ("str2 greater than str3");} else {document. write ("str2 less than str3 ");}
Result:
In the string comparison, the following letters are greater than the first letters, and the lower-case letters are greater than the upper-case letters.Use the built-in JavaScript function localecompare for comparisonLocalecompare is a wonderful thing. It depends on the sorting of the default region settings in the local system. You can just take a look at it.
VaR str1 = "ABC"; var str2 = "ABC"; if (str1.localecompare (str2) = 0) {document. Write ("str1 and str2 are equal! ");} If (str1.localecompare (str2) =-1) {document. Write (" str1 before str2! ");} If (str1.localecompare (str2) = 1) {document. Write (" str1 after str2! ");}
Result: Str1 is behind str2!
Now, let's write it here!
My Sina Weibo: X -- Zhang
Column address: Native Javascript
Thank you!
Text/X -- Zhang