Solve this: What's the Output of ('Cow' && 'Horse') in JavaScript?
JavaScript
Logic Building
Problem Solving

Solve this: What's the Output of ('Cow' && 'Horse') in JavaScript?

JavaScript Operators are the fundamentals to build logic and problem solving. Let's solve this interesting problem to understand how logical AND(&&) works.
3 min read

JavaScript provides four operators for logic building:

  • AND(&&)
  • OR(||)
  • NOT(!)
  • Nullish Coalescing (??)

To solve the problem in hand, i.e., what is the output of the expression ("Cow" && "Horse"), we need to understand the rules of the Logical AND(&&) operator.

The Logical AND(&&) Operator Rules

When we write expressions using any operator, the operator gets applied on one or more operands. For the Logical AND(&&) operator, we need at least two operands, like:

JS
Operand1 && Operand2

Now, the rules are:

  • The AND(&&) operator checks if the Operand1(the left-side/first operand) can be converted(or evaluated) to a false. If so, the output result will be Operand1.
  • Otherwise, the output result will be Operand2.

So, with these rules, we can confidently say:

  • The result of (false && false) will be false. Because, the first operand is false.
  • The result of (true && false) will be false. Because, the first operand is NOT false. So the second operand false will be returned.
  • The result of (true && true) will be true. Because, the first operand is NOT false. So the second operand true will be returned.
  • The result of (false && true) will be false. Because, the first operand is false.

You can also take a short-cut of these rules and say:

In case of the Logical AND(&&) operator, if any of the operand is false the output result will be false. Only when both the operands are true, then the result will be true.

That's great, but, the interviewers will confuse you with questions like:

What is the output of ("Cow" && "Horse")?

That's where you need to apply those two rules we learned earleir. Ask these questions to yourself:

  • Is "Cow" a boolen?
  • Nope, it is a string. Can it be converted to a false?
  • Nope. Even with JavaScript's implicit type conversion(we also call it coercion), the string "Cow" can not be converted to a boolean false.
  • Alright, so as per the rule, if the first operand can not be converted to a false, the second operand will be returned as the output result.
  • Hence, the "Horse" will be the output, Bingo!

So,

JS
console.log("Cow" && "Horse"); // Output will be "Horse"

Tips: When you learn anything fundamentally, you are more confident to tackle challenging situations in programming, including interviews.

Was it helpful? Would love to get your likes and comments below ☺️.

Want to learn further?

Learn about JavaScript Operators in-depth with fundamentals and examples.

Day 03: MASTER Operators & Expressions in JavaScript - In this session, we’ll learn about Operators and Expressions in JavaScript. This is your first step towards Problem Solving in JavaScript. This session will teach you with a lot of working examples and finish with real-life tasks and use cases.

Learn Full Stack

with tapaScript

By clicking Sign Up you re confirming that you agree with our Terms and Conditions.

newsletter

Don't forget to connect with us on