Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Compare Strings Using Mathematical Operators

const string1 = "hello"
const string2 = "world"

console.log(string1 > string2)
// false
/*
string1 is not greater than string2,
because h comes before w, so it is less than.

For the other example:
*/

const string1 = "banana"
const string2 = "back"

console.log(string1 > string2)
// true

/*
string1 is greater than string2 because ban comes after back.

And for the last example:
*/

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

console.log(string1 === string2)
// true

console.log(string1 < string3)
// false

/*
string1 is equal to (===) string2,
but string1 is not less than string3,
which is in contrast to localeCompare.

With mathematical operators, "fcc" is greater than "Fcc",
but with localeCompare, "fcc".localeCompare("Fcc")" returns -1
to show that "fcc" is less than "Fcc".

This behavior is one reason why I don't recommend using
mathematical operators for comparing strings,
even though it has the potential to do so.

Another reason why I don't recommend using mathematical
operators is because "fcc" > "fcc" and "fcc" < "fcc" is false.
"fcc" is equal to "fcc". So if you're depending on mathematical operators,
getting false may be for different reasons than you believe.

So, for comparing strings, amongst the many ways there may be,
using localCompare is an effective approach because
it can be used for different languages.
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: destructuring function nested parameters 
Javascript :: how to get faQuoteLeft fontawosome in react 
Javascript :: _.has Creator Functions Do Not Have "Constructor" 
Javascript :: weakset use cases javaScript 
Javascript :: jquery init dropdown 
Javascript :: changetypeprofiles 
Javascript :: angular reuse component with different data 
Javascript :: find parent index of nested array object javascript 
Javascript :: how to turn a time into a word js 
Javascript :: Constructor can also be written like this 
Javascript :: Use regular function with DOM event listeners, when using "this" keyword 
Javascript :: how to iterate through linked list javascript 
Javascript :: lwc format date js 
Javascript :: Load RequireJS Script 
Javascript :: pass data between componets in react 
Javascript :: apollo client multiple endpoints 
Javascript :: populate strapi v4 
Javascript :: mongoose lookup array of objects 
Javascript :: rpushx redis 
Javascript :: jsx children 
Javascript :: restart my react -Dom 
Javascript :: find the minimum number in an array javascript 
Javascript :: autoplay images in react js 
Javascript :: how to get first and last 
Javascript :: jquery show only first elements of table 
Javascript :: wordpress how to read jquery 
Javascript :: how to access property from inside an array 
Javascript :: (state.isLoggedIn = function() {return false}) react redux 
Javascript :: negative index javascript 
Javascript :: angularjs checking array of objects 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =