You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A switch statement can optionally have a default clause.
If present, it's usually the last clause, but it doesn't need to be. It is also allowed to put the default clause before all case clauses, or anywhere between. The behavior is mostly the same as if it was the last clause. The default block will be still executed only if there is no match in the case clauses (including those defined after the default), but there is also the ability to "fall through" from the default clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers.
Even if there is no "fall through" logic, it's still unexpected to see the default clause before or between the case clauses. By convention, it is expected to be the last clause.
If a switch statement should have a default clause, it's considered a best practice to define it as the last clause.
Rule Details
This rule enforces default clauses in switch statements to be last.
It applies only to switch statements that already have a default clause.
This rule does not enforce the existence of default clauses. See default-case if you also want to enforce the existence of default clauses in switch statements.
https://eslint.org/docs/rules/default-case-last
A
switch
statement can optionally have adefault
clause.If present, it's usually the last clause, but it doesn't need to be. It is also allowed to put the
default
clause before allcase
clauses, or anywhere between. The behavior is mostly the same as if it was the last clause. Thedefault
block will be still executed only if there is no match in thecase
clauses (including those defined after thedefault
), but there is also the ability to "fall through" from thedefault
clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers.Even if there is no "fall through" logic, it's still unexpected to see the
default
clause before or between thecase
clauses. By convention, it is expected to be the last clause.If a
switch
statement should have adefault
clause, it's considered a best practice to define it as the last clause.Rule Details
This rule enforces
default
clauses inswitch
statements to be last.It applies only to
switch
statements that already have adefault
clause.This rule does not enforce the existence of
default
clauses. See default-case if you also want to enforce the existence ofdefault
clauses inswitch
statements.Examples of incorrect code for this rule:
Examples of correct code for this rule:
The text was updated successfully, but these errors were encountered: