Search code examples
typescriptvariable-assignmentshort-circuit-evaluation

Order of operations in Typescript assignment using || and array .find


I am wanting to make sure that the operations in my assignment occur in a specific order. I am assuming it is in the order I listed below, but I have not been able to find documentation on typescript that confirms/denies this.

    const currentFreak =
      this.allFreaks.find(freak => freak.id === currentFreakId) ||
      this.allFreaks.find(freak => freak.Active) ||
      this.allFreaks.find(freak => freak.defaultFreakId);

Am I correct in assuming the above is executed in order, using short circuit logic? The documentation I found at https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining looks ambiguious on this specific topic in my reading of it.


Solution

  • it behaves as well as in javascript, so yes it follows short circuit logic. if there is a freak whose freak.id === currentFreakId the second .find method won't be called.