UrbanPro
true

Learn Java Script Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

String Comparison In Javascript

G
Guna
17/11/2017 0 0

String Comparison in javascript:

Let us learn how two strings are compared in javascript

Assume you have two strings:

"ant" and "ele"

Case 1:

alert("ant" < "ele" ); will give the result as true meaning "ant" is smaller than "ele". Because the first characters in string "ant" is "a" and in "ele" "e". "a" is considered small because "a" comes before "e". This makes the string "ant" smaller than "ele".

Case 2:

"ant" and "Ant"

alert("ant" < "Ant"); will give the result as false meaning "Ant" is smaller than "ant".

"A" is smaller than "a" it is because of unicode values that are used to represent character in computer.
a ---------> 97
A --------->65

Case 3:

"ant" and "ant"

alert("ant" < "ant" ); will give the result as false because they are equal if first characters are equal, compare the second characters the same way. Continue until the end of any string. If both strings ended simultaneously then they are equal,
here it is found to be same till the end so they are equal.

Case 4:

"ant" and "ants"

alert("ant" < "ants"); will give the result as true because if first characters are equal, compare the second characters the same way. Continue until the end of any string. If both strings do not end simultaneously the longer string is greater.

Summary:

  • Check the first characters of both strings.
  • If the first string has the first character as greater or less, then the first string is greater (or less) than the second. We are done.
  • If first characters are equal, compare the second characters the same way continue until the end of any string
  • If both strings ended simultaneously, then they are equal. Otherwise the longer string is greater.
0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Five exercises on numbers that will help you develop your logic
If you want to become a programmer (web developer) you need to have good logical thinking skills. Programming is all about planning and building logic. Here is a set of exercises that will help you develop...

NgModules vs. JavaScript modules (Angular 2)
The NgModule — a class decorated with @NgModule Decorator — is a fundamental feature of Angular. JavaScript also has its own module system for managing collections of JavaScript objects. It's...


Dependency Injection in Angular 2
Dependency injection Dependency injection is a way to supply a new instance of a class with the fully-formed dependencies it requires. Most dependencies are services. Angular uses dependency injection...

Angular 1 vs Angular 2
Angular team has released the angular 2, which has drastically change the way programmers interact and work with Angular framework in the past. In this post I will tell you the common changes made by...

Looking for Java Script Training classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Script Training Classes?

The best tutors for Java Script Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Script Training with the Best Tutors

The best Tutors for Java Script Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more