JavaScript supports logical operators that allow developers to compare values and make decisions based on the result of those comparisons. In this lesson, we will explore the three logical operators in JavaScript: AND (&&), OR (||), and NOT (!), and provide examples of how they can be used in code.
The AND operator returns true if both operands are true, and false otherwise. The syntax for the AND operator is &&. The following table shows the truth table for the AND operator:
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Example:
Loading...
In this example, the AND operator is used to check if both num1 and num2 are positive. If both operands are positive, the console will output "Both numbers are positive".
The OR operator returns true if at least one of the operands is true, and false otherwise. The syntax for the OR operator is ||. The following table shows the truth table for the OR operator:
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Example:
Loading...
In this example, the OR operator is used to check if at least one of num1 and num2 is positive. If at least one operand is positive, the console will output "At least one number is positive".
The NOT operator negates the value of its operand. If the operand is true, the NOT operator returns false. If the operand is false, the NOT operator returns true. The syntax for the NOT operator is !.
Example:
Loading...
In this example, the NOT operator is used to check if num1 is not greater than 10. If num1 is not greater than 10, the console will output "num1 is not greater than 10".
Logical operator precedence determines the order in which logical operators are evaluated in an expression. In JavaScript, logical operator precedence is as follows:
This means that NOT (!) is evaluated first, followed by AND (&&), and finally OR (||).
Example:
Loading...
In this example, the expression false || true && false is evaluated. According to the operator precedence rules, the AND operator (&&) is evaluated first, so true && false is evaluated, which returns false. Next, the OR operator (||) is evaluated, so false || false is evaluated, which returns false. Therefore, the value of result is false, and the console will output "false".
To avoid confusion and ensure that the expressions are evaluated as intended, developers can use parentheses to group the expressions in the desired order. For example:
Loading...
In this example, the parentheses group the OR operation (||) first, so false || true is evaluated, which returns true. Next, the AND operation (&&) is evaluated, so true && false is evaluated, which returns false. Therefore, the value of result is false, and the console will output "false".
Chain of operators, also known as a series of operators, refers to the use of multiple logical operators in an expression. In JavaScript, the order in which operators are evaluated is determined by their precedence, as we discussed earlier. However, when multiple operators have the same precedence, the order of evaluation is determined by their associativity.
In JavaScript, logical operators are left-associative, which means that expressions are evaluated from left to right. This means that if multiple operators have the same precedence, the operator on the left side of the expression is evaluated first.
Example:
Loading...
In this example, the expression false || true && false || true is evaluated. According to the operator precedence rules, the AND operator (&&) is evaluated first, so true && false is evaluated, which returns false. Next, the first OR operator (||) is evaluated, so false || false is evaluated, which returns false. Finally, the second OR operator (||) is evaluated, so false || true is evaluated, which returns true. Therefore, the value of result is true, and the console will output "true".
In conclusion, logical operators are an important aspect of programming that allows developers to make decisions based on the result of comparisons. JavaScript supports three logical operators: AND (&&), OR (||), and NOT (!). The AND operator returns true if both operands are true, the OR operator returns true if at least one operand is true, and the NOT operator negates the value of its operand. By following the operator precedence rules and using parentheses to group expressions when necessary, developers can avoid errors and create more reliable code.
Top Tutorials
Related Articles