Home > Web Front-end > JS Tutorial > body text

How to determine whether two strings are equal in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:51:04
Original
1706 people have browsed it

Convert all user input values ​​to uppercase (or lowercase) first, and then compare:

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


   if(name == "urname")


   {


     // statements go here.


   }

Copy after login



JavaScript has two equality operators. One is completely backward compatible, the standard "==". If the two operand types are inconsistent, it will automatically perform type conversion on the operand at some point. Consider the following assignment statement:

   var strA = "i love you!";


   var strB = new String("i love you!");

Copy after login



These two variables contain the same sequence of characters, but have different data types. The former is string and the latter is object. When using the "==" operator, JavaScript will try various evaluations to detect whether the two will be in the same sequence. equal in some cases. So the following expression evaluates to true: strA == strB.


The second operator is the "strict" "===", which is not so forgiving when evaluating and does not perform type conversion. So the expression strA === strB evaluates to 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 two options here: "!=" and strict "!==". Their relationship is similar to "==" and "===".


Discussion:


"==" and "!=" will try their best to find matching values ​​when evaluating, but you may still want to perform an explicit type conversion before comparing to "help" them do their job. For example, if you want to determine whether a user's input value (string) is equal to a number, you can let "==" help you complete the type conversion:

   if(document.form1.txtAge.value == someNumericVar) { ... }
Copy after login



You can also convert in advance:

   if(parseInt(document.form1.txtAge.value) == someNumericVar) { ... }
Copy after login



If you are more accustomed to strongly typed programming languages ​​(such as C#, Java, etc.), then you can continue your habit (type conversion) here, which will also enhance the readability of the program.

There is one thing you need to pay attention to, which is the computer’s regional settings. If you compare strings with "<" and ">", then JavaScript compares them as Unicode, but obviously people browsing the web don't read the text as Unicode :) For example in Spanish, According to traditional sorting, "ch" will be sorted as a character between "c" and "d". localeCompare() provides a way to use the character collation of the default locale.

  var strings; // 要排序的字符串数组,假设已经得到初始化


  strings.sort(function(a,b) { return a.localeCompare(b) }); // 调用sort()方法进行排序

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template