1.4 Testing Equality of Two Strings
NN 2, IE 3
1.4.1 Problem
You want to compare a
user's text entry against a known string value.
1.4.2 Solution
Convert the user input to either all uppercase or all lowercase
characters, and then use the JavaScript equality operator to make the
comparison:
if (document.myForm.myTextBox.value.toLowerCase( ) = = "new york") {
// process correct entry
}
By using the results of the case conversion method as one of the
operands of the equality expression, you do not modify the original
contents of the text box. (See Recipe 1.3 if you want to convert the
text in the text box to all of one case.)
1.4.3 Discussion
JavaScript has two types of equality operators. The fully
backward-compatible, standard equality operator (=
=) employs data type conversion in some cases when the
operands on either side are not of the same data type. Consider the
following variable assignments:
var stringA = "My dog has fleas.";
var stringB = new String("My dog has fleas.");
These two variables might contain the same series of characters but
are different data types. The first is a string value, while the
second is an instance of a String object. If you
place these two values on either side of an equality (=
=) operator, JavaScript tries various evaluations of the
values to see if there is a coincidence somewhere. In this case, the
two variable values would show to be equal, and the following
expression:
stringA = = stringB
returns true.
But the other type of equality operator, the strict equality operator
(= = =), performs no data type conversions. Given
the variable definitions above, the following expression evaluates to
false because the two object types differ, even
though their payloads are the same:
stringA = = = stringB
If the logic of your code requires you
to test for the inequality of two strings, you can use the inequality
(!=) and strict inequality (!=
=) operators. For example, if you want to process an
incorrect entry, the branching flow of your function would be like
the following:
if (document.myForm.myTextBox.value.toLowerCase( ) != "new york") {
// process incorrect entry
}
The same data type conversion issues apply to the inequality and
strict inequality operators as to their opposite partners.
Although the equality and inequality operators go to great lengths to
find value matches, you may prefer to assist the process by
performing obvious data type conversions in advance of the operators.
For instance, if you want to see if an entry to a numeric text box (a
string value) is a particular number, you could let the equality
operator perform the conversion for you, as in:
if (document.myForm.myTextBox.value = = someNumericVar) { ... }
Or you could act in advance by converting one of the operands so that
both are the same data type:
if (parseInt(document.myForm.myTextBox.value) = = someNumericVar) { ... }
If you are accustomed to more strongly typed programming languages,
you can continue the practice in JavaScript without penalty, while
perhaps boosting your script's readability.
1.4.4 See Also
Recipe 2.1 for converting between string and number values; Recipe 3.3 for converting between strings and arrays; Recipe 3.13 for
converting a custom object to a string value.
|