Car insurance is a must-have for all drivers, whether you...
Read MoreHow To Compare Strings In Javascript
There are many reasons to compare two strings. On top of that, there are many things to check when comparing two strings. For instance, if they are equal, higher or lower, if they are of the same data type etc. The comparison of two strings can be done in many ways. One can come up with a unique idea and write custom code to compare two strings to solve a specific problem they might be facing. However, in this article we are going to talk about two main ways to compare strings in JavaScript. When searching to hire JavaScript programmer you should check out vteams. They have pre-vetted javascript programmers for hire that make the hiring easy and fast.
Using localeCompare
The first method is using the local compare method. This method returns value 1 if the first word in string 1 comes after the first word in string 2. It returns -1 If one string has lowercase alphabets. And, returns 0 If both strings are equal in alphabetic order.
Here is a sample code:
const str1 = “hello”
const st2 = “world”
const result = str1.localeCompare(str2)
// -1
Why -1? Because the first letter of “str1” comes before the first letter of “str2”. Here is another example this time with results as 1
const str1 = “banned”
const str2 = “backed”
const result = str1.localeCompare(str2)
// 1
Why 1? Because, the “ban” comes after “bac”, like when you go to search for a word in a dictionary.
Sometimes, the code can return you -2 or some other negative value instead of just going for -1. But this does not change anything because the value still is negative. Similarly, if the value is positive regardless of the number it can be counted the same as “1”.
Using Mathematical Operators
When you thought of comparing strings your brain would have thought of mathematical operators like “=”,”<”,”>”. This is good because it works in a similar way and you are not wrong on this. Here is what your code would look like when using math operators.
const str1 = “hello”
const str2 = “worlds”
console.log(str1 > str2)
// false
console.log(str1 < str2)
// true
console.log(str1 == str2)
// false
const str1 = “abc”
const str2 = “Abc”
console.log(str1 === str2)
// false
The === operator compares the value as well as data type. Here the datatype is same but the values are not because A is capital in the second string.
There is a little problems you can run into when comparing two strings, especially like mentioned in the last example i.e. “abc” and “Abc” when using the mathematical operator. If you use a math operator to compare two such values it will tell that “abc” is greater than “Abc”. Inversely, when using localCompare the results are opposite as it tells that “Abc” is the greater one.
This is one of the reasons you developers use local Compare instead of mathematical operators. The reason behind it can be the use and implementation strategy of mathematical operators. This also can become a good interview question when you hire JS developers. To hire JavaScript developers you can now simply connect with vteams. We offer exceptionally talented and experienced JavaScript developers for hire at competitive pricing within 48 hours. So, what are you waiting for, contact us today and hire Javascript programmers you were searching for.