0

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 }

Gega
  • 11
  • 3
  • It doesn't, which can actually be useful for assigning a fallback: falsy_value || "fallback" // returns "fallback"; truthy_value || "fallback" // returns truthy_value; – Foxcode Dec 08 '22 at 11:00

1 Answers1

2

No, it does not

first() || second()

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

function second() {
  console.log('second')
  return false
}
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • 1
    Please look for duplicates prior to answering. There are over 2.4 million JavaScript questions on SO. Surely someone else has answered this question before? – Ivar Dec 08 '22 at 10:59
  • @Ivar Why is it bad to answer to a duplicate? – Konrad Dec 08 '22 at 11:52
  • 1
    Because we want to keep all the high quality information in one place. So that when people want more details/a different view on the matter can just check the other answers. So that when something in the language changes, it only needs to be updated in one place and not across thousands of questions. --> [Why are some questions marked as duplicate?](https://stackoverflow.com/help/duplicates) If a question that is closed as a duplicate prevents new answers from being accepted, you might argue that it shouldn't have been answered to begin with. – Ivar Dec 08 '22 at 11:58