When you compare two values via =, one of the values may undergo coercion.Ĭoercion– Automatically changing a value from one type to another.Īutomatically is the key word here. If you want to compare an int and String in Java, you must first convert them to the same type. 0 = "0" in Java, for example, returns this: error: incomparable types: int and String If you run 0 = "0" in your developer console, why does it return true?Ġ is a number, and "0" is a string, they should never be the same! Most programming languages respect that. With the spec in hand, let's deeply understand what's going on here. When these cases pop up, it's best to consult the source–the official ECMAScript specification that JavaScript is built from. "I don’t think anyone ever really knows JS, not completely anyway." Nonetheless, this corner case's details still nicked me. With experience, I've learned to embrace JavaScript's smooth sides while bewaring its prickly pines. Of course this little experiment lead me to wonder. The result isn't surprising, but still kind of disappointing. You can verify this meme's accuracy by running each code snippet in Developer Tools. I unintentionally found this JavaScript meme on Reddit, and it's the best one I've ever seen. TLDR: Coerce yourself to use triple equals.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |