JavaScript to determine whether two strings are equal to the method _ basics

Source: Internet
Author: User
Tags locale

First convert the user's input value to uppercase (or lowercase), and then compare:

   var name = Document.form1.txtUserName.value.toLowerCase ();


   if (name = = "Urname")


   {


     //statements go to here.


   }



JavaScript has two kinds of equality operators. One is completely backwards compatible, the standard "= =", if two operand types are inconsistent, it will automatically type the operand at some time, consider the following assignment statement:

   var stra = "I love you!";


   var strb = new String ("I love you!");



The two variables contain the same sequence of characters, but the data types are string, the latter is object, and when you use the "= =" operator, JavaScript tries various values to detect whether the two are equal in some case. So the following expression results in True:stra = = Strb.


The second operator is "strict" "= = =", it will not be so tolerant when the evaluation, will not be type conversion. So the value of the expression stra = = = Strb is False, although both variables hold the same value.


Sometimes the logic of the code requires you to determine whether two values are not equal, there are also two choices: "!=" and Strict "!==", their relationship is similar to "= =" and "= =".


Discuss:


"= =" and "!=" look for the matching of values as much as possible, but you might want to make explicit type conversions before you compare them to "help" them get the job done. For example, if you want to determine whether a user's input value (a string) equals a number, you can let "= =" help you complete the type conversion:

   if (Document.form1.txtAge.value = = Somenumericvar) {...}



You can also convert in advance:

   if (parseint (document.form1.txtAge.value) = = Somenumericvar) {...}



If you are more accustomed to strongly typed programming languages (such as C#,java, etc.), you can extend your habits (type conversions) here, which will also enhance the readability of the program.

One thing to note is the locale of the computer. If you use "<" and ">" to compare strings, JavaScript compares them as Unicode, but it is clear that people do not read text as Unicode when browsing the Web: In Spanish, for example, in traditional order, "ch" will be ranked as a character between "C" and "D". Localecompare () provides a way to help you use character collation under the default locale.

  var strings; An array of strings to sort, assuming that the initialization


  strings.sort (function (a,b) {return a.localecompare (b)}) is already initialized;//Call the sort () method to sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.