In JS, when using or (||) in if statement, when first is true, does it evaluated second? JavaScript behavior

246 Views Asked by At

In javascript, if (true || false) results in true, but the question is whether the compiler will evaluate the second statement if the first is true. In my case, I want to know if an array has changed, so I believe I have two options: compare the lengths of the arrays (what was and what is now) and compare if the array values are different.

I think the first option requires less work. If (first || second) { give me deleted values, and give me added values }

1

There are 1 best solutions below

3
Konrad On

No, it does not

first() || second()

function first() {
  console.log('first')
  return true
}

function second() {
  console.log('second')
  return false
}