You can use the localeCompare() method.

string_a.localeCompare(string_b);

/* Expected Returns:

 0:  exact match

-1:  string_a < string_b
 
 1:  string_a > string_b

 */

Further Reading:

  • Stack Overflow - Is there a JavaScript strcmp()?

  • Tutorials Point: JavaScript String - localeCompare() Method

  • How to sort strings in JavaScript

Answer from Daniel Vassallo on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › localeCompare
String.prototype.localeCompare() - JavaScript | MDN
Positive when the referenceStr occurs after compareString ... Warning: Do not rely on exact return values of -1 or 1! Negative and positive integer results vary between browsers (as well as between browser versions) because the ECMAScript specification only mandates negative and positive values. Some browsers may return -2 or 2, or even some other negative or positive value. ... // The letter "a" is before "c" yielding a negative value "a".localeCompare("c"); // -2 or -1 (or some other negative value) // Alphabetically the word "check" comes after "against" yielding a positive value "check".localeCompare("against"); // 2 or 1 (or some other positive value) // "a" and "a" are equivalent yielding a neutral value of zero "a".localeCompare("a"); // 0
🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
All the comparison operators above can also be used on strings: let text1 = "A"; let text2 = "B"; let result = text1 < text2; Try it Yourself » ... Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a ...
🌐
W3Schools
w3schools.com › jsref › jsref_localecompare.asp
JavaScript String localeCompare() Method
The current locale is based on the language settings of the browser. string.localeCompare(compareString) let text1 = "ab"; let text2 = "ab"; let result = text1.localeCompare(text2); Try it Yourself » · let text1 = "A"; let text2 = "a"; let result = text1.localeCompare(text2); Try it Yourself » · JavaScript Strings ·
🌐
freeCodeCamp
freecodecamp.org › news › string-equality-in-javascript-how-to-compare-strings-in-js
String Equality in JavaScript – How to Compare Strings in JS
November 7, 2024 - In this article, you will learn how to compare strings in JavaScript. Strict equality, or three equality (===) as its symbol implies, is a more detailed comparison than loose equality (==). It does not only check if the values are the same, but it also checks the operands:
🌐
JavaScript Tutorial
javascripttutorial.net › home › how to check if two strings are equal in javascript
How to Check if Two Strings are Equal in JavaScript
September 9, 2020 - const s1 = 'café'; const s2 = 'café'; console.log(s1===s2); // falseCode language: JavaScript (javascript) In this example, s1 and s2 look the same. But the comparison s1 === s2 evaluates to false. To understand how it works, you first need to know the concept of grapheme and combining characters. A grapheme is the smallest functional unit of a writing system. For example, the string café has four letters: c, a, f, and é (or e with acute).
Top answer
1 of 10
822

always Until you fully understand the differences and implications of using the == and === operators, use the === operator since it will save you from obscure (non-obvious) bugs and WTFs. The "regular" == operator can have very unexpected results due to the type-coercion internally, so using === is always the recommended approach.

For insight into this, and other "good vs. bad" parts of Javascript read up on Mr. Douglas Crockford and his work. There's a great Google Tech Talk where he summarizes lots of good info: http://www.youtube.com/watch?v=hQVTIJBZook


Update:

The You Don't Know JS series by Kyle Simpson is excellent (and free to read online). The series goes into the commonly misunderstood areas of the language and explains the "bad parts" that Crockford suggests you avoid. By understanding them you can make proper use of them and avoid the pitfalls.

The "Up & Going" book includes a section on Equality, with this specific summary of when to use the loose (==) vs strict (===) operators:

To boil down a whole lot of details to a few simple takeaways, and help you know whether to use == or === in various situations, here are my simple rules:

  • If either value (aka side) in a comparison could be the true or false value, avoid == and use ===.
  • If either value in a comparison could be of these specific values (0, "", or [] -- empty array), avoid == and use ===.
  • In all other cases, you're safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

I still recommend Crockford's talk for developers who don't want to invest the time to really understand Javascript—it's good advice for a developer who only occasionally works in Javascript.

2 of 10
270

If you know they are strings, then there's no need to check for type.

"a" == "b"

However, note that string objects will not be equal.

new String("a") == new String("a")

will return false.

Call the valueOf() method to convert it to a primitive for String objects,

new String("a").valueOf() == new String("a").valueOf()

will return true

Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
Determines whether the calling string contains searchString. ... Returns the index within this string of the first occurrence of searchValue, or -1 if not found. ... Returns a boolean indicating whether this string contains any lone surrogates. ... Returns the index within this string of the ...
🌐
Programiz
programiz.com › javascript › examples › string-comparison
JavaScript Program to Compare Two Strings
Become a certified JavaScript programmer. Try Programiz PRO! ... To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // js program to perform string comparison const string1 = 'JavaScript Program'; const string2 = 'javascript program'; // compare both strings const result = string1.toUpperCase() === string2.toUpperCase(); if(result) { console.log('The strings are similar.'); } else { console.log('The strings are not similar.'); }
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Comparisons
Don’t use comparisons >= > < ... separately. Comparison operators return a boolean value. Strings are compared letter-by-letter in the “dictionary” order....
🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-comparison-how-to-compare-strings-in-js
JavaScript String Comparison – How to Compare Strings in JS
July 1, 2022 - By Dillion Megida You may want to compare two strings to know which is higher or lower alphabetically or to see if they are equal. You can do this in many ways. I'll show you two of them in this article. 1. How to Compare Strings Using localeCompare ...
🌐
Latenode
community.latenode.com › other questions › javascript
How to properly compare strings in JavaScript? - JavaScript - Latenode Official Community
April 8, 2025 - Hey everyone, I’m new to JavaScript and I’m a bit confused about string comparison. What’s the best way to check if two strings are the same in JS? I’ve seen different methods like == and ===, but I’m not sure which one …
🌐
Dmitri Pavlutin
dmitripavlutin.com › compare-javascript-strings
Is it Safe to Compare JavaScript Strings?
Firstly, you are safe to compare strings that contain characters from Basic Multilangual Plane (including the ASCII characters) using regular comparison operators ===, == or utility function Object.is().
🌐
Medium
medium.com › @rahul.tpointtech12 › comparing-strings-in-javascript-what-you-need-to-know-f62800a96d34
Comparing Strings in JavaScript: What You Need to Know | by Rahul | Medium
March 21, 2025 - In JavaScript, the simplest way to compare two strings is by using the equality operators (== and ===). The == operator checks for equality after performing type conversion, while the === operator checks for strict equality, meaning it does ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Equality
Equality (==) - JavaScript | MDN
This can be roughly summarized as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Equality_comparisons_and_sameness
Equality comparisons and sameness - JavaScript | MDN
Number to BigInt: compare by their mathematical value. If the number is ±Infinity or NaN, return false. String to BigInt: convert the string to a BigInt using the same algorithm as the BigInt() constructor. If conversion fails, return false. Traditionally, and according to ECMAScript, all primitives and objects are loosely unequal to undefined and null. But most browsers permit a very narrow class of objects (specifically, the document.all object for any page), in some contexts, to act as if they emulate the value undefined.
🌐
Hyperskill
hyperskill.org › learn › step › 18295
String Compare
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
🌐
Flexiple
flexiple.com › javascript › javascript-string-comparison-methods
JavaScript String Comparison – How to Compare Strings in JS - Flexiple
var string1 = "apple"; var string2 = "banana"; var string3 = "apple"; var isEqualOrLess = string1 <= string2; // Evaluates to true var isEqualOrGreater = string1 >= string3; // Evaluates to true · In this example, isEqualOrLess evaluates to true because "apple" is lexicographically less than "banana". Similarly, isEqualOrGreater evaluates to true as "apple" is equal to "apple". These operators facilitate sorting operations and quick checks for range inclusion in JavaScript, enabling efficient lexicographical order comparisons in strings.
🌐
TutorialsPoint
tutorialspoint.com › how-to-do-case-sensitive-string-comparison-in-javascript
How to do case-sensitive string comparison in JavaScript?
The "strict equality operator (===)" is used to compare strings based on their values and character case. Use "comparison operators" along with the "length" property to compare strings based on how long they are.