Equality (==) versus Identity (===)
If we would like to include type in our comparison, we must use the identity operator (===). Now, using the same arguments as the previous example, the expression 1 === ‘1’ would result in false.
In the case of non-primitives (i.e. objects), the reference is always used for comparison, thereby eradicating the operator discrepancy.
It is best practice to use the identity operator (commonly referred to as “strict equality”) in all cases to avoid unforeseen behavior and potential errors caused by having the incorrect type. Any linter should be configured as such.
Null (null) versus Undefined (undefined)
Considering this, it is best practice to avoid explicitly setting an identifier to undefined as it can lead to ambiguity as to whether the value was previously set. Both null and undefined evaluate to false in a Boolean context.
Truthy and Falsy values
Truthy values are best described as anything that is not falsy. Falsy values are precisely false, 0, ‘’ (empty string), NaN (not a number), undefined, and null. Notice how they correlate to fundamental types of the language – number, string, Boolean, and object. These values may be used directly in conditional expressions (e.g. ternary operator or if-statement) without the need for further operators due to type conversion highlighted earlier.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.