
Understanding Logical Operators (|| and &&) in JavaScript
The given code uses logical OR (||) and logical AND (&&) operators with different values. Let’s break them down carefully.
🔹 First Expression:
Step 1: Understanding the || (Logical OR) Operator
- The logical OR (
||) operator returns the first truthy value it encounters. - If all values are falsy, it returns the last falsy value.
Step 2: Evaluating the Expression (false || null || "Hello")
We evaluate from left to right:
falseis falsy → move to the next operand.nullis also falsy → move to the next operand."Hello"is truthy → STOP and return"Hello".
➡ Output of First Expression:
🔹 Second Expression:
Step 1: Understanding the && (Logical AND) Operator
- The logical AND (
&&) operator returns the first falsy value it encounters. - If all values are truthy, it returns the last truthy value.
Step 2: Evaluating the Expression (false && null && "Hello")
We evaluate from left to right:
falseis falsy → STOP and returnfalse(no need to check the rest).nulland"Hello"are ignored because&&stops at the first falsy value.
➡ Output of Second Expression:
Remember key points:
|
✔ Final Answer:
A) Hello false
This question appeared in the Mindtree 2nd round of interview, testing the candidate’s knowledge of Logical Operators like (&& and ||) operator in JavaScript.