0

Today i see something like this

const a = 1 && 2 && 3 && 4

And when we console.log this it will return 4

As my understand, i think it will return true mean, 1 ,2 ,3 ,4 do not consider falsy value, so it will return true, but this make me think about condition

From when i learn javascript, i always read condition like this:

if(1 &&2 ) {
   return true
}

So i think 1&&2 will return true, apply for above code, why it return 4 ?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
CODEforDREAM
  • 863
  • 1
  • 8
  • 24
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#using_and – Jonathan May 12 '22 at 08:01
  • 2
    *'So i think 1&&2 will return true...'* it returns 2. From the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#description): *'Logical AND (&&) evaluates operands from left to right, returning immediately with the **value** of the first falsy operand it encounters; if all values are truthy, the **value** of the last operand is returned.'* – pilchard May 12 '22 at 08:03
  • 2
    @RokoC.Buljan simply because it is evaluated as truthy the statement '1&&2 will return true' is still not accurate. It returns 2 which is evaluated by the `if` (not the &&) as a boolean and so found to be truthy. – pilchard May 12 '22 at 08:11

0 Answers0