Compare

JavaScript String Compare

Comparing strings in JavaScript is quite easy, as long as you know about the equals operator and the JavaScript If Statement. This is all you need to know to find out if two strings of your choosing are equal.

Comparing one String to another String

Below we have created a fake authentication system and use an if statement to see if the user’s name will grant them access to a special message.

JavaScript Code:

<script type="text/javascript">
var username = "Agent006";
if(username == "Agent007")
document.write("Welcome special agent 007");
else
document.write("Access Denied!");
document.write("<br /><br />Would you like to try again?<br /><br />");

// User enters a different name
username = "Agent007";
if(username == "Agent007")
document.write("Welcome special agent 007");
else
document.write("Access Denied!");

</script>

Display:

Access Denied!

Would you like to try again?

Welcome special agent 007

Be sure you realize that when you are comparing one string to another, you use two equals operators “==” instead of just one “=”. When you use two equals operators, it means you are comparing two values.

In this case, the English translation of our program would be: “If username is equal to Agent007, then print out a welcome message; otherwise, access is denied.”

Case Insensitive String Compare

Above, we used a case sensitive compare, meaning that if the capitalization wasn’t exactly the same in both strings, the if statement would fail. If you would like to just check that a certain word is entered, without worrying about the capitalization, use the toLowerCase function.

JavaScript Code:

<script type="text/javascript">
var username = "someAgent";
if(username == "SomeAgent")
document.write("Welcome special agent");
else
document.write("Access Denied!");

// Now as case insensitive
document.write("<br /><br />Let's try it with toLowerCase<br /><br />");
if(username.toLowerCase() == "SomeAgent".toLowerCase())
document.write("Welcome special agent");
else
document.write("Access Denied!");
</script>

Display:

Access Denied!

Let’s try it with toLowerCase

Welcome special agent

By converting both strings to lowercase, we were able to remove the problem of it failing to find a match when the capitalization was slightly off. In this case, the “s” was capitalized in the username, but not capitalized in our if statement check.

Obtain the most prominent rss wordpress plugin offered today, you'll locate both free and exceptional plugins right here. We have examined the whole web, reviewed hundreds of advised Wordpress plugins and chosen the most effective.
Get computer technology news the most recent innovation information, consisting of new product releases, sales figures and tech business performance information. Review guides on brand-new gizmos and models for future modern technology.
google seo tips that you must and should not be doing on your Websites to make them rank greater in search engines. In a frequently transforming Search Engine Optimization landscape, it is important to remain current about the changing techniques and methods of optimization.
Below is a list of software managing multiple WordPress in a central place. There are WordPress control panels, and also multisite tools to take care of domains, themes, campaigns, and more. consists of one-click gain access to, tracking, backup, implementation, posting, and safety components. Testimonial which of your websites have themes and plugins that need focus. With one click, upgrade all your plugins, styles or core WordPress software application.


My Name is Adi Saputra. I come from Indonesia. I want to develop www.tuto-rial.com Blog Being the best, So much of it as a reference in the world of IT in particular programming

Comments are closed.