Two or more relations can be logically joined using the logical operators AND and OR. Logical operators combine relations according to the following rules: - The ampersand (&) symbol is a valid substitute for the logical operator AND. The vertical bar ( | ) is a valid substitute for the logical operator OR.
- Only one logical operator can be used to combine two relations. However, multiple relations can be combined into a complex logical expression.
- Regardless of the number of relations and logical operators used to build a logical expression, the result is either true, false, or indeterminate because of missing values.
- Operators or expressions cannot be implied. For example, X EQ 1 OR 2 is illegal; you must specify X EQ 1 OR X EQ 2.
- The ANY and RANGE functions can be used to simplify complex expressions.
AND . Both relations must be true for the complex expression to be true. OR . If either relation is true, the complex expression is true. The following table lists the outcomes for AND and OR combinations. Table 1. Logical outcomesExpressionOutcomeExpressionOutcome true AND true = true true OR true = true true AND false = false true OR false = true false AND false = false false OR false = false true AND missing = missing true OR missing = true* missing AND missing = missing missing OR missing = missing false AND missing = false* false OR missing = missing * Expressions where the outcome can be evaluated with incomplete information. See the topic Missing values in logical expressions for more information. There are four logical operators in JavaScript: if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 (OR), if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 (AND), if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 5 (NOT), if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 6 (Nullish Coalescing). Here we cover the first three, the if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 6 operator is in the next article.Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type. Let’s see the details. || (OR)The “OR” operator is represented with two vertical line symbols: result = a || b;
In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8, it returns if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8, otherwise it returns let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 0.In JavaScript, the operator is a little bit trickier and more powerful. But first, let’s see what happens with boolean values. There are four possible logical combinations: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false
As we can see, the result is always if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8 except for the case when both operands are let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 0.If an operand is not a boolean, it’s converted to a boolean for the evaluation. For instance, the number let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 3 is treated as if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8, the number let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 5 as let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 0:if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
}
Most of the time, OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 is used in an let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8 statement to test if any of the given conditions is if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8.For example: let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
}
We can pass more conditions: let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
}
OR "||" finds the first truthy valueThe logic described above is somewhat classical. Now, let’s bring in the “extra” features of JavaScript. The extended algorithm works as follows. Given multiple OR’ed values: result = value1 || value2 || value3;
The OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 operator does the following:A value is returned in its original form, without the conversion. In other words, a chain of OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 returns the first truthy value or the last one if no truthy value is found.For instance: alert( 1 || 0 ); // 1 (1 is truthy)
alert( null || 1 ); // 1 (1 is the first truthy value)
alert( null || 0 || 1 ); // 1 (the first truthy value)
alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)
This leads to some interesting usage compared to a “pure, classical, boolean-only OR”. Getting the first truthy value from a list of variables or expressions. For instance, we have let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
} 4, let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
} 5 and let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
} 6 variables, all optional (i.e. can be undefined or have falsy values).Let’s use OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 to choose the one that has the data and show it (or let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
} 8 if nothing set):let firstName = "";
let lastName = "";
let nickName = "SuperCoder";
alert( firstName || lastName || nickName || "Anonymous"); // SuperCoder
If all variables were falsy, let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
} 8 would show up.Short-circuit evaluation. Another feature of OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 operator is the so-called “short-circuit” evaluation.It means that if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.The importance of this feature becomes obvious if an operand isn’t just a value, but an expression with a side effect, such as a variable assignment or a function call. In the example below, only the second message is printed: true || alert("not printed");
false || alert("printed");
In the first line, the OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 operator stops the evaluation immediately upon seeing if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8, so the result = value1 || value2 || value3; 4 isn’t run.Sometimes, people use this feature to execute commands only if the condition on the left part is falsy.
&& (AND)The AND operator is represented with two ampersands if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4:result = a && b;
In classical programming, AND returns if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 8 if both operands are truthy and let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 0 otherwise:alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 0An example with let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8:alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 1Just as with OR, any value is allowed as an operand of AND: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 2AND “&&” finds the first falsy valueGiven multiple AND’ed values: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 3The AND if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 operator does the following:In other words, AND returns the first falsy value or the last value if none were found. The rules above are similar to OR. The difference is that AND returns the first falsy value while OR returns the first truthy one. Examples: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 4We can also pass several values in a row. See how the first falsy one is returned: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 5When all values are truthy, the last value is returned: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 6Precedence of AND if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 is higher than OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 The precedence of AND if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 operator is higher than OR if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3.So the code alert( 1 || 0 ); // 1 (1 is truthy)
alert( null || 1 ); // 1 (1 is the first truthy value)
alert( null || 0 || 1 ); // 1 (the first truthy value)
alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) 5 is essentially the same as if the if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 expressions were in parentheses: alert( 1 || 0 ); // 1 (1 is truthy)
alert( null || 1 ); // 1 (1 is the first truthy value)
alert( null || 0 || 1 ); // 1 (the first truthy value)
alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) 7.Don’t replace let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8 with if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3 or if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 Sometimes, people use the AND if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 operator as a "shorter way to write let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8".For instance: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 7The action in the right part of if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 would execute only if the evaluation reaches it. That is, only if let firstName = "";
let lastName = "";
let nickName = "SuperCoder";
alert( firstName || lastName || nickName || "Anonymous"); // SuperCoder 4 is true.So we basically have an analogue for: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 8Although, the variant with if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 appears shorter, let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8 is more obvious and tends to be a little bit more readable. So we recommend using every construct for its purpose: use let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8 if we want let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
} 8 and use if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 if we want AND.! (NOT)The boolean NOT operator is represented with an exclamation sign if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 5.The syntax is pretty simple: alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false 9The operator accepts a single argument and does the following: - Converts the operand to boolean type:
true || alert("not printed");
false || alert("printed"); 1. - Returns the inverse value.
For instance: if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 0A double NOT true || alert("not printed");
false || alert("printed"); 2 is sometimes used for converting a value to boolean type:if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 1That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. There’s a little more verbose way to do the same thing – a built-in true || alert("not printed");
false || alert("printed"); 3 function:if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 2The precedence of NOT if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 5 is the highest of all logical operators, so it always executes first, before if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 4 or if (1 || 0) { // works just like if( true || false )
alert( 'truthy!' );
} 3.
What kind of statement allows us to run one block of code if one condition is true and a separate block of code otherwise?
The if/else statement
With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.
When you decisions because the resulting action requires that two conditions be true you must decide which of the two decisions to make first?
An AND decision can be constructed using a nested decision, or a nested if—that is, a decision “inside of” another decision. When you nest decisions because the resulting action requires that two conditions be true, you must decide which of the two decisions to make first.
What type of operator can be used to determine whether a specific relationship?
A relational operator determines whether a specific relationship exists between two values.
When you need to satisfy two or more criteria to initiate an event in a program you must make sure?
When you need to satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely independently of the first decision. Any decision can be made using combinations of just two types of comparisons: equal and not equal.
|