Skip to content

defaultCaseLast

Reports switch statements where the default clause is not last.

✅ This rule is included in the ts logical preset.

When a default clause appears before other case clauses in a switch statement, it can lead to confusion about the control flow. While the default clause will still only execute when no other case matches, placing it in the middle or at the beginning makes the code harder to understand. Convention and readability are improved by keeping the default clause at the end of switch statements.

function getDescription(value: number): string {
switch (value) {
default:
return "unknown";
case 1:
return "one";
case 2:
return "two";
}
}
function processValue(input: string): void {
switch (input) {
case "start":
console.log("Starting...");
break;
default:
console.log("Processing...");
break;
case "end":
console.log("Ending...");
break;
}
}

This rule is not configurable.

If you have a specific code style guide that requires default clauses to appear in a different position, you might choose to disable this rule. However, this is generally not recommended as it goes against common JavaScript conventions.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.