Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Compare Strings Using localeCompare

/* locaelCompare returns:

1 if string1 is greater (higher in the alphabetical order) than string2
-1 if string1 is smaller (lower in the alphabetical order) than string2
0 if string1 and string2 are equal in the alphabetical order

*/

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

const compareValue = string1.localeCompare(string2)
// -1

/*
It gives -1 because, in the English locale,
h in hello comes before w in the world
(w is further down in the alphabetical order than h)
*/

const string3 = "banana"
const string4 = "back"

const compareValue = string3.localeCompare(string4)
// 1

/*
The comparison above gives 1 because, in the English locale,
ban in banana comes after bac in back.
*/

const string5 = "fcc"
const string6 = "fcc"
const string7 = "Fcc"

const compareValue1 = string5.localeCompare(string6)
// 0

const compareValue2 = string5.localeCompare(string7)
// -1

/*
Comparing "fcc" and "fcc" gives 0 because they are equal in order.
"fcc" and "Fcc" gives -1 because capital "F" is greater than small "f".
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: radio button form validation 
Javascript :: Remove the minimum 
Javascript :: Underscore _.create() Function 
Javascript :: Function Returning This 
Javascript :: element non empty jquer y check 
Javascript :: react jsx hello react sample 
Javascript :: draft save using jquery 
Javascript :: Below Means Person is A Constructor Function 
Javascript :: Backbone Initialize Arguments 
Javascript :: Prototype Constructor Will Be Class or Function 
Javascript :: When defined as a method of an object, in a regular function this refers to the object 
Javascript :: how to Play/start or pause timer in javascript 
Javascript :: using condition how to disable radio button in angular 
Javascript :: How to Solve the Staircase Problem with JavaScript using Memoization 
Javascript :: vuejs router Cannot GET /about 
Javascript :: react native scan network 
Javascript :: mvc return view with query string 
Javascript :: javascript Program for sum of arithmetic series using loop 
Javascript :: react Dark/Light mode 
Javascript :: regex from 5 to 30 1 number 1 lower case and 1 upper case letter 
Javascript :: for loop increment by more than one 
Javascript :: prime number in javascript using for loop 
Javascript :: firebase hosting rewrite function You need to enable JavaScript to run this app. 
Javascript :: swift urlsession remote json 
Javascript :: samesite cookies/console.log 
Javascript :: getting-host-is-not-configured-error-when-using-next-image 
Javascript :: if this then this, else that 
Javascript :: node js rest with flutter 
Javascript :: laravel , json Why it shows Cannot access offset of type string on string error 
Javascript :: angularjs GetVideos API, Cant get the key parameter inside the array 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =