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

Closures in Javascript
A closure is the combination of a function and the lexical environment within which that function was declared. Consider the following example: function makeFunc() { var name = 'Mozilla'; function...


Some good resources
Clean Code by Robert C Martin, is available in my profile please download and read it, irrespective of what language you code in, it will change the way you code. Also if you code in Javascript read https://12factor.net/ https://blog.risingstack.com/javascript-clean-coding-best-practices-node-js-at-scale/ For...

What Is Mean Stack? Why Mean Stack Is Popular Now?
Mean Stack is standing for MongoDB, ExpressJS, AngularJS and NodeJS. These four javascript solutions create a high performing web application. Mainly JavaScript is the frontend language. It’s working...

JavaScript Arrow Function.
Javascript Arrow Functions : JavaScript ES6 is a type or version which let us provide a way to write javascript syntax on a better way. ES6 version is having a feature or new method to use functions...
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