This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:javascript-best-practices/no-unreachable
Language: JavaScript
Severity: Warning
Category: Best Practices
Description
In JavaScript, any code following a return, throw, continue, or break statement is unreachable and will never be executed. The only exception here is declarations, which are valid because of JavaScript Hoisting.
Non-Compliant Code Examples
functionfn(){x=1;returnx;x=3;// this will never execute
}classCextendsB{#x;// this will never be added to instances
constructor(){return{};}}functionfoo(){returntrue;console.log("done");}functionbar(){thrownewError("Oops!");console.log("done");}functionloop(){while(value){break;console.log("done");}}functionerror(){thrownewError("Oops!");console.log("done");}functionbaz(){if(Math.random()<0.5){return;}else{thrownewError();}console.log("done");}functionanotherLoop(){for(;;){}console.log("done");}switch(foo){case1:break;x=2;}classCextendsB{#x;// unreachable
#y=1;// unreachable
a;// unreachable
b=1;// unreachable
constructor(){return{};}}