JavaScript is supported in operators ||
(Or), &&
(and) and !
(NOT).
They are called “logical” , but in JavaScript they can be applied to values of any type and also return values of any type.
||
(OR)
The OR operator looks like a double vertical bar symbol:
Logical OR in classical programming works as follows: “if at least one of the arguments is true
, then it returns true
, otherwise, false
”.
It turns out the following table of results:
When calculating OR in JavaScript, you can use any values. In this case, they will be interpreted as logical.
For example, the number 1
will be taken as true
, and 0
as false
:
Usually the OR operator is used in if
to check if at least one of the conditions is met, for example:
More conditions can be passed:
Short cycle of calculations
JavaScript calculates several OR from left to right. At the same time, in order to save resources, the so-called “short calculation cycle” is used .
Suppose several OR are calculated in a row: a || b || c || ...
a || b || c || ...
a || b || c || ...
If the first argument is true
, then the result will certainly be true
(at least one of the values is true
), and the other values are ignored.
This is especially noticeable when the expression passed as the second argument has a third-party effect — for example, assigns a variable.
When you run the example below, assignment x
will not occur:
... And in the example below, the first argument is false
, so OR will try to calculate the second, thereby triggering the assignment:
OR value
So, as we can see, the OR operator calculates exactly as many values as necessary - before the first true
.
The OR operator returns the value at which the calculation stopped.
Examples:
It is used, in particular, to select the first “true” value from the list:
Importance: 3
Answer: first 1
, then 2
.
The call to alert
does not return a value, or, in other words, returns undefined
.
- First operator OR
||
will execute the first alert(1)
, get undefined
and go on to the second operand. - Since the second operand
2
is true, the calculation will end with the result undefined || 2
undefined || 2
will be 2
, which will be displayed by an external alert( .... )
.
The second operator ||
will not be executed, execution to alert(3)
will not reach, therefore 3
will not be displayed.
[Open task in new window]
&&
(AND)
The AND operator is written as two ampersands &&
:
In classical programming, and returns true
if both arguments are true, otherwise false
Example:
As in OR, any values are allowed:
The same principle of “short cycle of calculations” applies to And, but a bit differently than to OR.
If the left argument is false
, the AND operator returns it and ends the calculation. Otherwise, it calculates and returns the right argument.
For example:
Importance: 3
Answer: 1
, undefined
.
The call to alert
does not return a value, or, in other words, returns undefined
.
Therefore, it won't get to the right alert
, the calculations will end on the left.
[Open task in new window]
The priority of the operator AND &&
greater than OR ||
i.e. it is executed before.
Therefore, in the following code, the right AND will be calculated first: 1 && 0 = 0
, and only then - OR.
The &&
operator in simple cases can be used instead of if
, for example:
The action on the right side of &&
will be executed only if calculations reach it. That is, if the left side is true
.
It turned out analog:
However, as a rule, if
better read and understood. It is more obvious, therefore it is better to use it. This, however, also applies to other non-obvious uses of language features.
!
(NOT)
The operator is NOT the easiest. He gets one argument. Syntax:
Actions !
:
- First, the argument is boolean
true/false
. - Then returns the opposite value.
For example:
In particular, doubles are NOT used to convert values to a logical type:
Importance: 3
if (age >= 14 && age <= 90) |
[Open task in new window]
Importance: 3
First option:
if ( !(age >= 14 && age <= 90) ) |
The second option:
if (age < 14 || age > 90) |
[Open task in new window]
Importance: 5
Answer: the first and third will be executed. Details:
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone