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...

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...

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...

Mark And Sweep Algorithm in Javascript Memory Management
This algorithm reduces the definition of "an object is not needed anymore" to "an object is unreachable". This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root...

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...
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