Dokumentacja języka JavaScript 1.5:Operatory:Operatory logiczne
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
Spis treści |
[edytuj] Podsumowanie
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
| Operatory | |
| Zaimplementowana w: | JavaScript 1.0 |
| Wersja ECMA: | ECMA-262 |
Operatory logiczne są opisane w następującej tabeli:
| Operator | Zastosowanie | Opis |
|---|---|---|
Logiczny AND (&&) |
expr1 && expr2 |
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false. |
Logiczny OR (||) |
expr1 || expr2 |
Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false. |
Logiczny NOT (!) |
!expr |
Returns false if its single operand can be converted to true; otherwise, returns true. |
Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), or undefined.
Even though the && and || operators can be used with operands that are not Boolean values, they can still be considered Boolean operators since their return values can always be converted to Boolean values.
[edytuj] Short-Circuit Evaluation
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
-
false && anythingis short-circuit evaluated to false. -
true || anythingis short-circuit evaluated to true.
The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.
[edytuj] Kompatybilność wsteczna
[edytuj] JavaScript 1.0 i 1.1
Operatory && i || zachowują się następująco:
| Operator | Usage | Behavior |
|---|---|---|
&& |
expr1 && expr2 |
If the first operand (expr1) can be converted to false, the && operator returns false rather than the value of expr1. |
|| |
expr1 || expr2 |
If the first operand (expr1) can be converted to true, the || operator returns true rather than the value of expr1. |
[edytuj] Przykłady
[edytuj] Operator logiczny AND (&&)
Następujący kod pokazuje przykłady operatora logicznego && (AND).
a1=true && true // t && t zwraca true a2=true && false // t && f zwraca false a3=false && true // f && t zwraca false a4=false && (3 == 4) // f && f zwraca false a5="Cat" && "Dog" // t && t zwraca Dog a6=false && "Cat" // f && t zwraca false a7="Cat" && false // t && f zwraca false
[edytuj] Operator logiczny OR (||)
Następujący kod pokazuje przykłady operatora logicznego || (OR).
o1=true || true // t || t zwraca true o2=false || true // f || t zwraca true o3=true || false // t || f zwraca true o4=false || (3 == 4) // f || f zwraca false o5="Cat" || "Dog" // t || t zwraca Cat o6=false || "Cat" // f || t zwraca Cat o7="Cat" || false // t || f zwraca Cat
[edytuj] Operator logiczny NOT (!)
Następujący kod pokazuje przykłady operatora logicznego ! (NOT).
n1=!true // !t zwraca false n2=!false // !f zwraca true n3=!"Cat" // !t zwraca false