Rule: unnecessary-else

Disallows else blocks following if blocks ending with a break, continue, return, or throw statement.

Rationale

When an if block is guaranteed to exit control flow when entered, it is unnecessary to add an else statement. The contents that would be in the else block can be placed after the end of the if block.

Config

You can optionally specify the option "allow-else-if" to allow “else if” statements.

Config examples
"unnecessary-else": true
"unnecessary-else": [true, {"allow-else-if": true}]
Schema
{
  "type": "object",
  "properties": {
    "allow-else-if": {
      "type": "boolean"
    }
  }
}

Code examples:

Disallows "else" following "if" blocks ending with "return", "break", "continue" or "throw" statement.
"rules": { "unnecessary-else": true }
Passes
if (someCondition()) {
    return;
}
// some code here

if (someCondition()) {
    continue;
}
// some code here

if (someCondition()) {
    throw;
}
// some code here

if (someCondition()) {
    break;
}
// some code here
Fails
if (someCondition()) {
    return;
} else {
    // some code here
}

if (someCondition()) {
    break;
} else {
    // some code here
}

if (someCondition()) {
    throw;
} else {
    // some code here
}

if (someCondition()) {
    continue;
} else {
    // some code here
}