![]() ![]() 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 In the above code snippet we have given different values of same type to the variable a and to the operator, so the result gives 'true'.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. In the above code snippet we have given different value and different type to the variable a and to the operator, so the result gives 'true'.Īssigning different value and same type Assigning different value and same type to the operator gives the result asĭocument.getElementById(" myId").innerHTML = ( a != 20) In the above code snippet we have assigned same value and same type to the variable a and to the operator, so the result gives 'false'.Īssigning differnt value and different type Assigning different value and different type to the operator gives the result asĭocument.getElementById(" myId").innerHTML = ( a != "20") In the above code snippet we have given same value to the variable a and to the operator, so the result gives 'true'.Īssigning same value and same type Assigning same value and same type to the operator gives the result asĭocument.getElementById(" myId").innerHTML = ( a != "30") Differnt value and same type gives result 'true'.Īssigning same value and different type Assigning equal value and differnt type to the operator gives the result asĭocument.getElementById(" myId").innerHTML = (a != "30").Diffrent value and differnt type gives result 'true'. ![]() Same value and same type gives result 'false'.Same value and different type gives result 'true'.In this any one should be different either value or type. The symbolic representation of Not equal value or Not equal type is !=. Not equal value or Not equal type is an comparison operator which is used to check whether the two operands are having not equal value or not equal type. In the previous post we learnt about the Equal value and Equal type, now we shall learn the Not equal value or Not equal type.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |