DEV Community

Cover image for Conditionally Comparing Strings in JavaScript
M.Ark
M.Ark

Posted on • Updated on

Conditionally Comparing Strings in JavaScript

Comparing two strings to see if they’re the same.
Use the equality operator (==) within a conditional test:

var strName = prompt("What's your name?", "");
if (strName == "Shelley") {
 alert("Your name is Shelley! Good for you!");
} else {
 alert("Your name isn't Shelley. Bummer.");
}
Enter fullscreen mode Exit fullscreen mode

Two strings can be compared using the equality operator (==). When used within a conditional statement, a block of code is run if the test evaluates to true (the strings are equal):

if (strName == "Shelley") {
 alert("Your name is Shelley! Good for you!");
}

Enter fullscreen mode Exit fullscreen mode

If the strings are not equal, the first statement following the conditional statement block is processed. If an if...else conditional statement is used, the block of code following the else keyword is the one that’s processed:

if (strName == "Shelley") {
 alert("Your name is Shelley! Good for you!");
} else {
 alert("Your name isn't Shelley. Bummer.");
}
Enter fullscreen mode Exit fullscreen mode

Factors that can influence the success of the string comparison:
Strings have case, and can consist of uppercase characters, lowercase characters, or a combination of both
Unless case is an issue, you’ll most likely want to convert the
string to all lowercase or uppercase, using the built-in String methods toLowerCase and toUpperCase, before making the comparison, as shown in the following code:

var strName = prompt("What's your name?", "");
if (strName.toUpperCase () == "SHELLEY") {
 alert("Your name is Shelley! Good for you!");
} else {
 alert("Your name isn't Shelley. Bummer.");
}
Enter fullscreen mode Exit fullscreen mode

**Note **that the toUpperCase method (and toLowerCase) do not take any parameters.
Comparison operators work numerically with numbers, but lexically with strings.
For instance, the value “dog” would be lexically greater than “cat”, because the letter “d” in “dog” occurs later in the alphabet than the letter “c” in “cat”:

var sOne = "cat";
var sTwo = "dog"
if (sOne > sTwo // false, because "cat" is lexically less than "dog"
Enter fullscreen mode Exit fullscreen mode

If two string literals only vary based on case, the uppercase characters are lexically greater than the lowercase letter:

var sOne = "Cat";
var sTwo = "cat";
if (sOne >= sTwo) // true, because 'C' is lexically greater than 'c'
Enter fullscreen mode Exit fullscreen mode

There is no strict greater than or strict less than operators, so it makes no difference if the data type of the operands differs:

var sOne = new String("cat");
var sTwo = "cat";
if (sOne <= sTwo) // both equal, so true, as data type doesn't matter
Enter fullscreen mode Exit fullscreen mode

Finally, The localeCompare method takes one parameter, a string, which is compared against the string value to which it is attached.
The method returns a numeric value equal to 0 if the two strings are the same; –1 if the string parameter is lexically greater than the original string; 1 otherwise:

var fruit1 = "apple";
var fruit2 = "grape";
var i = fruit1.localeCompare(fruit2); // returns -1
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tanvir1407 profile image
Info Comment hidden by post author - thread only accessible via permalink
MD. Tanvir

copy from JavaScript Cookbook

Some comments have been hidden by the post's author - find out more