Skip to content

single return vs multiple returns #761

@petersendidit

Description

@petersendidit

I see some examples of multiple returns but don't see any guidelines defined that say it explicitly.
Is there a preference between a single return where a variable is assigned the output value vs just having multiple return statements?

function isGood(battery) {
  if (battery.charge) {
    return true;
  }

  if (battery.cycles < 10) {
    return true;
  }

  return false;
}

vs.

function isGood(battery) {
  let out = false;
  if (battery.charge) {
    out = true;
  }

  if (battery.cycles < 10) {
    out = true;
  }

  return out;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions