Lecture
null
and undefined
In this section, we will introduce the comparison operators and the logical values that such operators return.
Many comparison operators are familiar to us from school:
a > b
, a < b
. a >= b
, a <= b
. a == b
. '='
. One character a = b
would mean assignment. ≠
, in JavaScript - an equal sign with an exclamation mark in front of it !=
. Like other operators, the comparison returns a value. This value has a special logical type.
There are only two logical values:
true
- it makes sense "yes", "true", "truth". false
- means “no”, “false”, “false”. For example:
1 | alert( 2 > 1 ); // true, верно |
2 | alert( 2 == 1 ); // false, неверно |
3 | alert( 2 != 1 ); // true |
Logical values can be used directly, assign variables, work with them as with any other:
1 | var a = true ; // присвоили явно |
2 | var b = 3 > 4; // false |
3 |
4 | alert( b ); // false |
5 |
6 | alert( a == b ); // (true == false) неверно, результат false |
Strings are compared letter by letter:
1 | alert( 'Б' > 'А' ); // true |
Letters are compared alphabetically. What letter in the alphabet later is the one and more.
An analogue of the “alphabet” in the internal representation of strings is the encoding, each character has its own number (code). JavaScript uses Unicode encoding. This compares the numeric character codes .
In Unicode, the lowercase letter is usually larger than the lowercase letter, so:
1 | alert( 'а' > 'Я' ); // true, строчные буквы больше прописных |
For correct comparison, the characters must be in the same register.
The comparison is carried out as in the phone book or in the dictionary. First, the first letters are compared, then the second, and so on, until one is more than the other.
In other words, more is the line that in the phone book would be on a larger page.
For example:
1 | alert( 'Банан' > 'Аят' ); |
1 | alert( 'Вася' > 'Ваня' ); // true, т.к. 'с' > 'н' |
1 | alert( 'Привет' > 'Прив' ); // true, так как 'е' больше чем "ничего". |
Such a comparison is called lexicographic .
Usually we get the values from the visitor as strings. For example, prompt
returns the string that the visitor entered.
The numbers obtained in this way cannot be compared in the form of strings, the result will be incorrect. For example:
1 | alert( "2" > "14" ); // true, неверно, ведь 2 не больше 14 |
In the example above, 2
turned out to be more than 14
, because the strings are compared character by character, and the first character '2'
greater than '1'
.
It would be correct to convert them to the number explicitly. For example, putting in front of them +
:
1 | alert( + "2" > + "14" ); // false, теперь правильно |
When comparing values are converted to numbers. Exception: when both values are strings, then they are not converted.
For example:
1 | alert( '2' > 1 ); // true |
2 | alert( '01' == 1 ); //true |
3 | alert( false == 0 ); // true, false становится 0, а true 1. |
The topic of type conversions will be continued further in the chapter Object Conversions: toString and valueOf.
Normal equality cannot distinguish 0
from false
:
1 | alert(0 == false ); // true, т.к. false преобразуется к 0 |
What to do if you still need to distinguish 0
from false
?
To test equality without type conversion, the strict equality operators are used ===
(triple equals) and !==
.
They compare without type conversion. If the type is different, then such values are always unequal (strictly):
1 | alert(0 === false ); // false, т.к. типы различны |
null
and undefined
Problems with special values are possible when the comparison operation is applied to the variable > < <= >=
, and it can have both a numerical value and null/undefined
.
Intuitively, it seems that null/undefined
equivalent to zero, but this is not so! They behave differently.
null
and undefined
are equal ==
each other and are not equal to anything else. null
becomes 0
, and undefined
becomes NaN
. Let's see funny consequences.
null
- 0
comparison result Compare null
with zero:
1 | alert( null > 0); // false |
2 | alert( null == 0); // false |
So, we got that null
no more and is not zero. And now…
1 | alert( null >= 0); // true |
How is this possible? If something is “greater than or equal to zero,” then it is reasonable to believe that it is either more or equal . But here it is not.
The fact is that equality ==
and comparison >= > < <=
algorithms work differently.
Comparison honestly leads to a number, it turns out zero. And when checking equality, null
and undefined
values are processed in a special way: they are equal to each other, but not equal to something else.
The result is a strange from the point of view of common sense situation, which we saw in the example above.
undefined
The value undefined
cannot be compared at all:
1 | alert( undefined > 0); // false (1) |
2 | alert( undefined < 0); // false (2) |
3 | alert( undefined == 0); // false (3) |
(1)
and (2)
give false
because undefined
when converted to a number, gives NaN
. And the NaN
value according to the standard is arranged so that comparisons ==
, <
, >
, <=
, >=
and even ===
with it return false
. (3)
gives false
, because the standard explicitly states that undefined
is only null
and nothing else. Conclusion: any comparison with undefined/null
, except for exact ===
, should be done with caution. It is advisable not to use comparisons >= > < <=
, in order to avoid errors in the code.
true
(true) and false
(false). Comparison operators return them. ===
( !==
). null
and undefined
are equal ==
each other and are not equal to anything else. In other comparisons (with the participation of >
, <
), it is better not to use them, since they do not behave as 0
.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone