0

I was asked in an interview that what would be logged by the following:

  1. console.log(1||2);//logs 1
    
  2. console.log(1&&2);//logs 2
    

DEMO

console.log(1||2,1&&2)

But Why? How are they evaluated?

I am sorry if its a duplicate :(

vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
  • Why not googling that ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators, see "logical operators" section, everything is explained – Pierre May 06 '18 at 08:06
  • to understand this you need to know how expression get evaluated inside condition block, for `1||2` javascript considers `1` to be `true` so it won't evaluate ` || 2`, hence returns and logs `1`. while `1&&2` is `and` operator so first it evaluates `1` and moves to ` && 2` it is also true so last evaluated value is `2` hences logs `2` – Rajmani Arya May 06 '18 at 08:06
  • I would say the first one is rather obvious, since 1 equals to true and `||` operator looks only for first true operand. 1 is true, so there is no need to move further. To evaluate the second operand. The next example is a bit more complicated, I would bet, that since both operands are true, only the last one is going to be log. – kwiat1990 May 06 '18 at 08:09

2 Answers2

2

That is how those logical operators are implemented in Javascript.

Intuition behind their working I think can be explained by looking at how short circuit evaluation works.

For example with ||, if you know the left hand side is truthy there is no need to check the right hand side (because the result will be true anyway) - so it directly returns what's on the left hand side.

For &&, it is not enough that left hand side is truthy, one must also check right hand side. If right hand side is also truthy it returns what is on the right hand side.

More details here.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • Short-circuit evaluation is a separate concept from this. – JJJ May 06 '18 at 08:05
  • @JJJ Check edit, does this answer make more sense? – Giorgi Moniava May 06 '18 at 08:09
  • You're still tying the expression's value to short-circuit evaluation, which, again, is a separate concept and doesn't directly determine the value. It just means that the interpreter doesn't do "unnecessary" work evaluating parts of the expression when the value has already been determined. – JJJ May 06 '18 at 08:10
  • @JJJ the answer to OPs question is that the behavior he/she sees is because of both short. circ. eval. and the semantics JS has (e.g. that it chose to return the first truthy value in case of ||) – Giorgi Moniava May 06 '18 at 08:18
  • There's no short-circuiting involved in `1&&2`, and `1||2` can be (and is) *defined* to return the left side whether or not short-circuiting is involved or not. – JJJ May 06 '18 at 08:27
  • @JJJ What I am saying is that in 1 || 2 the behavior to not check the right hand side when the left hand side is true is a behavior also seen in short circuit evaluation in logic. – Giorgi Moniava May 06 '18 at 08:32
1

One important thing in javascript is that everything (not only booleans) can be either truthy, or falsy. Everything except null, undefined, 0 and false is truthy, these are falsy.

 if(1) alert("truthy");
 if(0) alert("falsy");

Now the logical operators are defined as:

||: returns the left side if it is truthy, otherwise it returms the right side. That also works as expected with booleans:

 false || false // false
 false || true // true
 0 /*falsy */ || 1 /*truthy*/ // 1
 1 /*truthy*/ || 0 // 1

&&: returns the first if it is falsy, otherwise the right one:

 false && true // false
 0 && 1 // 0
 1 && 2 // 2
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151