Logical Operators
JavaScript Fundamentals: Logical Operators
What makes the logical || (OR), && (AND), ! (NOT) operators unique?
View Answer:
Interview Response: You can apply logical operators to values of any type; logical operators are not limited to a Boolean type.
If an operand is not a Boolean, what does the logical || OR do?
View Answer:
Interview Response: If an operand is not a Boolean, it converts to a Boolean for the evaluation. For instance, the number 1 gets treated as true, and the number 0 is false.
Code Example:
/* Logical OR returns the first truthy value,
* or the last value if there are none
*/
console.log('1' || '0'); // returns 1
console.log(1 || 0); // returns 1
console.log(0 || 0); // returns 0
How does logical OR “||” function?
View Answer:
Interview Response: The OR || operator does the following:
- Operands evaluate from left to right.
- Converts each operand to a Boolean value, and if the result is true, the program terminates and returns the operand's original value.
- Returns the final operand if all operands get evaluated (i.e., all were false).
Code Example:
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)
note
Logical OR “||” finds the first truthy value or the last value if there are none.
What is the definition of a short-circuit evaluation in JavaScript?
View Answer:
Interview Response: When JavaScript evaluates an OR expression, JavaScript short-circuits by not proceeding to the second operand if the first operand is true.
Code Example:
true || alert('not printed');
false || alert('printed');
What does the Logical && (AND) return?
View Answer:
Interview Response: Logical && (AND) returns true if both operands are truthy and false otherwise.
Code Example:
alert(true && true); // true
alert(false && true); // false
alert(true && false); // false
alert(false && false); // false
What type of conversion does the Logical && (AND) perform?
View Answer:
Interview Response: Logical && (AND) converts all operands to Boolean values, true or false.
What steps does JavaScript perform when using the Logical && (AND) operator?
View Answer:
Interview Response: The AND && operator performs the following functions:
- Operands evaluate from left to right.
- Converts each operand to a Boolean value, and if the result is false, the program terminates and returns the operand's original value.
- It returns the final operand if all operands get evaluated (i.e., all were true).
Code Example:
// if the first operand is truthy,
// AND returns the second operand:
alert(1 && 0); // 0
alert(1 && 5); // 5
// if the first operand is falsie,
// AND returns it. The second operand is ignored
alert(null && 5); // null
alert(0 && 'no matter what'); // 0
What are the differences between Logical || (OR) and Logical && (AND) operators?
View Answer:
Interview Response: The difference is that AND returns the first falsie value while OR returns the first truthy.
Which of the two, Logical || (OR) and && (AND) operators have the higher operator precedence?
View Answer:
Interview Response: The AND operator has higher precedence than the OR operator.
Technical Response: The precedence of the AND && operator is greater than that of the OR || operator. As a result, the code a && b || c && d is effectively the same as if the && expressions were enclosed in parentheses: (a && b) || (c && d).
Can you replace conditional if with OR (||) or && (AND)?
View Answer:
Interview Response: Technically, Yes, but it is not recommended because it reduces readability.
Technical Response: Technically, we can do it, which goes against recommendations. Although the variant with && appears shorter, an if statement is more prominent and tends to be more readable.
Code Example:
let x = 1;
x > 0 && alert('Greater than zero!');
// the if example is cleaner and obvious
let x = 1;
if (x > 0) alert('Greater than zero!');
What steps does JavaScript perform when implementing the Logical! (NOT) operator?
View Answer:
Interview Response: The operator accepts a single argument, converts it to a Boolean, and returns the inverse value.
Technical Response: The operator accepts a single argument and does the following:
- Converts the operand to a Boolean type: true/false.
- Returns the inverse value.
Code Example:
alert(!true); // false
alert(!0); // true
What happens when we apply the !! (DOUBLE NOT) to a value?
View Answer:
Interview Response: The value is inverted twice, returning it to its original Boolean value.
Technical Response: The first NOT changes the value to Boolean and returns the inverse, whereas the second NOT reverses the process. Finally, we have a simple Value-to-Boolean translation (Boolean conversion).
Code Example:
alert(!!'non-empty string'); // true
alert(!!null); // false
Is there a built-in object that performs in the same fashion as the !! (DOUBLE NOT) operator?
View Answer:
Interview Response: The Boolean object produces the same result as the double not.
Technical Response: The built-in Boolean object performs this in the same fashion as the !! (DOUBLE NOT) operator.
Code Example:
alert(!!'non-empty string'); // true
alert(!!null); // false
// Example of the Boolean method
alert(Boolean('non-empty string')); // true
alert(Boolean(null)); // false
What has the highest operator precedence over all the logical operators?
View Answer:
Interview Response: Logical NOT is the highest of all logical operators. It always executes first.
Technical Response: The precedence of NOT ! is the highest of all logical operators, so it always executes first, before && (AND) or || (OR).