What is the correct way to check for equality between Strings in JavaScript? Show
asked Aug 27, 2010 at 17:37
8 always Until you fully understand the differences and implications of using the 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 (
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. answered Aug 27, 2010 at 17:39
STWSTW 43.5k17 gold badges106 silver badges157 bronze badges 18 If you know they are strings, then there's no need to check for type.
However, note that string objects will not be equal.
will return false. Call the valueOf() method to convert it to a primitive for String objects,
will return true answered Aug 27, 2010 at 17:40
AnuragAnurag 138k36 gold badges219 silver badges257 bronze badges 6 Just one addition to answers: If all these methods return false, even if strings seem to be equal, it is possible that there is a whitespace to the left and or right of one string. So, just put a
I have lost hours trying to figure out what is wrong. Hope this will help to someone! answered May 2, 2017 at 13:41
akelecakelec 3,5193 gold badges40 silver badges38 bronze badges 8 You can use a == b (and its negation !=) a === b (and its negation !==) answered Aug 3, 2020 at 19:44
Kamil KiełczewskiKamil Kiełczewski 75.1k26 gold badges335 silver badges310 bronze badges 5 what led me to this question
is the check my case
and title was
answered Jul 27, 2016 at 10:52
1 There are actually two ways in which strings can be made in javascript.
So the best way to check for equality is using the If you want to check for equality between two objects then using
answered May 2, 2015 at 19:11
AbhishekAbhishek 1,1961 gold badge8 silver badges12 bronze badges String
Dunc 17.6k6 gold badges81 silver badges99 bronze badges answered Jul 4, 2018 at 12:33
Muhammad UsmanMuhammad Usman 7951 gold badge10 silver badges18 bronze badges 1 Strict ComparisonsTo do simple comparison, use
Alphabetical ComparisonsIf you
want to compare two strings to know if a string comes before or after another string, based on natural sorting, use the
answered May 26, 2021 at 15:47
HoldOffHungerHoldOffHunger 16.6k8 gold badges91 silver badges121 bronze badges Considering that both strings may be very large, there are 2 main approaches I recommed this function
answered Feb 25, 2020 at 10:25
NagibabaNagibaba 3,2891 gold badge31 silver badges37 bronze badges Also consider that answered Apr 19 at 20:09
grssnbchrgrssnbchr 2,8256 gold badges35 silver badges69 bronze badges For strings, we have a supported method Usage:
answered Aug 7 at 8:17
Not the answer you're looking for? Browse other questions tagged javascript string string-comparison or ask your own question.Can you use == when comparing strings?You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
How do you compare two strings in JavaScript?To compare two strings in JavaScript, use the localeCompare() method. The method returns 0 if both the strings are equal, -1 if string 1 is sorted before string 2 and 1 if string 2 is sorted before string 1.
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() . Both str1 and str2 contain ASCII characters, so you can safely compare them using comparison operators.
Should you ever use == in JavaScript?Short answer: never. This post looks at five possible exemptions from the rule to always use === and explains why they aren't. JavaScript has two operators for determining whether two values are equal [1]: The strict equality operator === only considers values equal that have the same type.
|