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.

Answer from STW on Stack Overflow
🌐
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 - This now returns “-1” because “c” which is the first character of string2 on the left side comes before “f”. When both strings are equal, it returns “0” irrespective of their positions:
🌐
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).
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Equality_comparisons_and_sameness
Equality comparisons and sameness - JavaScript | MDN
The behavior for performing loose equality using == is 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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Comparisons
Otherwise, if both strings’ first characters are the same, compare the second characters the same way. Repeat until the end of either string. If both strings end at the same length, then they are equal.
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

🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0.
🌐
TutorialKart
tutorialkart.com › javascript › javascript-check-if-strings-are-equal
How to Check if Strings are Equal in JavaScript?
December 6, 2022 - To check if two strings are equal in JavaScript, use equal-to operator == and pass the two strings as operands. The equal-to operator returns a boolean value
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_localecompare.asp
JavaScript String localeCompare() Method
❮ Previous JavaScript String Reference Next ❯ · Compare "ab" with "cd": let text1 = "ab"; let text2 = "cd"; let result = text1.localeCompare(text2); Try it Yourself » · let text1 = "cd"; let text2 = "ab"; let result = text1.localeCompare(text2); Try it Yourself » · More examples below. The localeCompare() method compares two strings in the current locale. The localeCompare() method returns sort order -1, 1, or 0 (for before, after, or equal).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-compare-two-strings
JavaScript - Compare Two Strings - GeeksforGeeks
July 23, 2025 - We can use Regular Expression to verify if two given strings are same or not. However, this method is not case sensitive. ... let str1 = "Hello"; let str2 = "hello"; let regex = new RegExp(`^${str2}$`, 'i'); if (regex.test(str1)) { console.log("Equal"); } else if (str1 > str2) { console.log("str1 is greater"); } else { console.log("str2 is greater"); }
🌐
TutorialsPoint
tutorialspoint.com › how-to-do-case-sensitive-string-comparison-in-javascript
How to do case-sensitive string comparison in JavaScript?
Step 2 ? Check if the first string, str1 is exactly equal to another string, str2. Step 3 ? Based on the condition check result, display the required output. In the below example, we compare two strings ? "Tutorials Point" and "Tutorials Point". We define a function "compare()" to compare two strings ?str1' and ?str2'. We compare the strings using the strict inequality. <html> <body> <h4> Case-sensitive string comparison in JavaScript</h4> <p id="result1"> </p> <p id="result2"> </p> <p id="result3"> </p> <script> function compare(str1, str2){ if (str1 === str2 ) { return true; } else { return false; } } var str1 = "Tutorials Point"; var str2 = "Tutorials point"; document.getElementById("result1").innerHTML = str1 document.getElementById("result2").innerHTML = str2 document.getElementById("result3").innerHTML = compare(str1, str2) </script> </body> </html>
🌐
Dmitri Pavlutin
dmitripavlutin.com › compare-javascript-strings
Is it Safe to Compare JavaScript Strings?
Is it always the case that 2 strings looking the same are equal? Let's try another example: ... While str1 and str2 look the same, the comparison str1 === str2 evaluates to false. How's that possible? Let's detail into how to correctly compare strings in JavaScript.
🌐
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 ...
🌐
Flexiple
flexiple.com › javascript › string-comparison
JavaScript String Comparison – How to Compare Strings in JavaScript - Flexiple
This process involves comparing ... to perform string comparisons, such as the equality operator (==), strict equality operator (===), and methods like localeCompare()....
🌐
Altcademy
altcademy.com › blog › how-to-compare-strings-in-javascript
How to compare strings in JavaScript
June 9, 2023 - The equality operator (==) checks whether two values are equal, and returns true if they are, or false if they're not. When comparing strings, this means that it checks whether the two strings have the same sequence of characters.
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically. ... If you want to use W3Schools services as an educational institution, ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › what-is-the-correct-way-to-check-for-string-equality-in-javascript
Check if Strings are Equal in JavaScript - GeeksforGeeks
July 23, 2025 - This operator checks for value equality but not type equality. we are checking the equality of the strings by using the double equals(==) operator.
🌐
Flexiple
flexiple.com › javascript › javascript-string-comparison-methods
JavaScript String Comparison – How to Compare Strings in JS - Flexiple
In JavaScript, developers compare strings to determine if they are identical, often using the === operator for strict comparison. JavaScript evaluates string equality based on each character's sequence and encoding.