I have always seen that JavaScript
there is:
- assignments
=
- comparisons
==
and===
I understand that it ==
does something similar to compare the value of the variable and ===
it also compares the type (like a java equals).
Could someone confirm this point for me and extend it? . I'm a Javanese and sometimes I love javascript untyping and sometimes I hate it.
What is the correct way in javascript to compare undefined
, null
and other default values?
variable == null
variable === null
Is undefined
it used as a text string or as a keyword? Which of the following comparisons is correct for an element html
without value
? (for example a label without content)
variable == "undefined"
variable === "undefined"
variable == undefined
variable === undefined
The operators
===
and are the strict!==
comparison operators . This means that if the operands have different types, they are not the same. For example,The operators
==
and are the relaxed!=
comparison operators . That is, if the operands have different types, JavaScript tries to convert them so that they are comparable. For example,It is worth mentioning that the operator
==
is not transitive , unlike===
.It's not very easy to remember all the rules of relaxed comparison, and sometimes it works in a counterintuitive way. Therefore, I recommend using
===
instead of==
.I don't remember all the little details of the operator
==
, so let's take a look at the spec, point 11.9.3:This answer is a translation of my answer to the same question on the Russian site .
The difference is that == first tries to convert the types before comparing them. The === operator does not, it does a direct comparison although it always returns false if the types are different. source (in English)
ex:
These graphs may help you visualize the difference:
'==' operator
Operator '==='
( source of images )
It effectively
==
compares value, while===
also comparing type. For example.You can read the technical details (in English) in Equality comparisons and sameness
undefined
is a reserved word and is used without quotes.Not quite. It is true that the operator
===
compares the type and the value, without doing conversions as the operator would==
. However that is only true for value types. In reference types, what it does is verify that it is the same object, it is not enough that it is of the same type and value. So it===
would be an identity operator, as opposed to==
.For example:
The string case would work as a value type, unless you used the constructor
String
, which would be a reference type.Also note that the operator
==
is not transitive in some conversions. For example:Given the ambiguity of this operator, it is advised that the operator be used
===
whenever possible.In any case, you can take a look at this page that will resolve doubts regarding both operators. There you can check among other things that
undefined == null
butundefined !== null
.very simple
==
it is equality of value and not of type.===
is equality of value and type.Your question has already been answered correctly, but I would like to make a comment on what seems to be your problem (I also started with java and there are certain things that javascript makes easy).
A variable can have 2 states, "undefined" or have something, null as in SQl is a value type, that is to say that if your variable has null it is defined and has something, nothing but something :P , undefined means that your variable has not been defined, this fact is curious especially because null == undefined responds true.
Now when you search for an element with js and you don't find it (for example document.getElementById("inpt")) its value is null, in the case that if you find it you ask for its value and if it doesn't have it it answers "" .
The interesting part is that with a simple if you solve a lot.
In case you don't transpile your JS code (with Babel for example) the "==" operation is faster than the type check with "===" therefore if your code is going to be used many times (it is part of the architecture or some library) it is preferable to use "==" considering that you are not going to pass a type filter. In all other cases (the vast majority) it is better to use type checking with "===" to avoid unexpected results.
Let's clarify concepts:
Every variable in JavaScript has a type and a value .
In JavaScript there is something called coercion . Coercion is an automatic (and implicit) conversion that the JavaScript engine performs in an attempt to operate on values of different types. For example:
Since value1 is of type string and value2 is of type number , JavaScript performs the automatic conversion of the second value in an attempt to be able to perform the sum. In this way, the value of the variable
suma
will be of type string :CONCLUSION
The operator
==
compares two values regardless of type. That is, it coerces and then compares. If the values are the same after conversion, return the valuetrue
.The operator
===
instead returnstrue
only in the case where value and type match.Clarification: this only applies to the 6 primitive types.