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

Web Technology Tutorial
Web Technology has 2 category Front End (HTML,CSS,JS Etc) Back End/Server Side (PHP,JSP,ASP,ROR Etc) Front End technologies are - HTML, CSS, Bootstrap, Javascript, jQuery etc. Using front end...

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

JavaScript Arrow Function
Javascript Arrow Functions : JavaScript ES6 is a type or version which let us provide a way to write javascript syntax in a better way. ES6 version is having a feature or new method to use functions...
A

Abhay

0 1
0

What is hoisting in Javascript?
As a matter of fact it is, javascript is an interpreted language and the javascript interpreter performs a lot of mystical stuff behind the scenes. Hoisting in JavaScript is certainly one of them. A quick...

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...
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