If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions) otherwise, return false.ġ2. If x is the same number value as y, return true.ġ1. If Type(x) is not Number, go to step 11.ħ. If Type(x) is different from Type(y), return false.Ĥ. Such a comparison is performed as follows:ġ. The comparison x = y, where x and y are values, produces true or false. The Strict Equals Operator ( = ), and after wading through the specificationalese I find:ġ1.9.6 The Strict Equality Comparison Algorithm Searching the PDF file for = brings me to page 56 of the specification: 11.9.4. So, if you can read a specification, this will help you in your profession tremendously. I see that in this very old topic nobody had an answer for the very strange effect. Please let me explain how to read the specification to clarify this question. But it is helpful to understand why new String("a") != "a". Of course in practice it matters more how the most important browsers behave than an esoteric definition of how something is supposed to be handled. Just remember that creating a string object using the String constructor creates something of type Object that behaves as a string most of the time.ĮCMA-262 is the specification for a scripting language of which JavaScript is a dialect. a is of type Object, while b is of type string. In this case a and b are not the same type. I thought strings behave like value types? Well, it depends who you ask. Now it becomes interesting: var a = "12" + "3" Īlert(a = b) // returns true, because strings behave like value typesīut how about this?: var a = new String("123") Īlert(a = b) // returns false !! (but they are equal and of the same type) Strings are not value types, but in Javascript they behave like value types, so they will be "equal" when the characters in the string are the same and when they are of the same length (as explained in the third rule) There's more to the story than being equal and being of the same type.Ī = b returns true if a and b have the same value and are of the same typeĪ = b returns true if a and b reference the exact same objectĪ = b returns true if a and b are both strings and contain the exact same characters Var ab_eq = (a = b) // false (even though a and b are the same type) So, let's take the following code: var a = It actually means that both operands reference the same object, or in case of value types, have the same value. Some will say that = means equal and of the same type, but that's not really true. In the answers here, I didn't read anything about what equal means. My advice is to bypass the question entirely and just don't use the String constructor to create string objects from string literals. Which one is correct? That really depends on what you're trying to compare. Here the = operator is checking the values of the two objects and returning true, but the = is seeing that they're not the same type and returning false. For example, consider the comparison of a string primitive with a string object created using the String constructor. The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its toString or valueOf method. For objects, = and = act consistently with one another (except in a special case). All of the comparisons just shown produce false with the = operator.Ī good point was brought up by in the comments and in Laybaert's answer concerning objects. My advice is to never use the evil twins. These are some of the interesting cases: '' = '0' // false The rules by which they do that are complicated and unmemorable. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. If the two operands are of the same type and have the same value, then = produces true and != produces false. The good ones work the way you would expect. JavaScript has two sets of equality operators: = and !=, and their evil twins = and !=. To quote Douglas Crockford's excellent JavaScript: The Good Parts, The = operator will not do the conversion, so if two values are not the same type = will simply return false. The = operator will compare for equality after doing any necessary type conversions. Reference: JavaScript Tutorial: Comparison Operators The strict equality operator ( =) behaves identically to the abstract equality operator ( =) except no type conversion is done, and the types must be the same to be considered equal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |