4 answers

★ Best answer

The problem usually isn't with ===, but with hidden characters like spaces, line breaks, or different Unicode normalizations that aren't visible in the debugger. Try applying `.trim()` to both strings and check their `.length` - if they're different, you've found your problem. If that doesn't help, you can use `.charCodeAt()` to check each individual character, or output the strings in the console with `console.log()` and copy them to make sure there's really nothing hidden.

In JavaScript, `===` compares the reference, not the content - if the strings come from different sources (e.g., one from an input field, the other from an API), they can have different memory locations and `===` will return false. Invisible characters like spaces, line breaks, or special Unicode characters can also cause two visually identical strings to not be equal.

What exactly are you trying to do - are you comparing user inputs, API responses, or something else? First, I'd check both strings for length using `console.log(str1.length, str2.length)` and then look at `console.log([...str1])` or `JSON.stringify()` to see if there are hidden characters in there. Sometimes `str1.trim()` helps if it's a whitespace issue, or `str1.toLowerCase()` if capitalization is the problem.

If the strings really are identical and `===` still doesn't work (which shouldn't actually happen), then tell me how you're actually creating or loading the values - then we can dig deeper. Some APIs also return weirdly encoded strings or they're number strings instead of actual strings (`"123"` vs `123`).

Anna asker I'm comparing inputs from input fields with values from an API that I'm loading. I'll try the length checks right away! 🎣

Try using `trim()`, especially if the strings come from input fields or APIs - invisible whitespaces at the beginning or end like to hide there, and the debugger won't show them clearly. Different line breaks (`\n` vs. `\r\n`) can also be the issue, particularly when data comes from different systems. If that doesn't help, you could output both strings with `console.log()` as JSON (so `console.log(JSON.stringify(string1))`) - then you'll definitely see every single character and quickly find where the differences are.

I've stumbled over that one plenty of times, and I can tell you: it rarely actually comes down to `===` itself. The other answers talk about references, but that's not really the issue with strings - JavaScript interns strings and `===` works reliably there. The real problem is hidden characters that the debugger just doesn't show you.

My tip: try running `string1.charCodeAt(0)` and `string2.charCodeAt(0)` in the console and compare the number sequences. That way you'll immediately see if there are any weird Unicode characters in there. And yeah, `trim()` is really often the solution - especially with input fields or API responses, whitespace, line breaks, or even null characters hide in there. `toLowerCase()` can help too if the capitalization is different and you missed it.

If none of that works, check the lengths with `string1.length` vs `string2.length`. If they're different, you'll know right away that something's hidden. And if both strings really are identical (even with `charCodeAt`), then they could be coming from different data types - try checking with `typeof`.

Your answer

Log into answer.