See the following example where case B is undesirable and either we want case A or case C. I could envisage this being detected by (i) (ii) or (iii) below. (iii) is possible with rule “no-var” but I don’t want to go through and change all of my code base.
function throw1 (j) {
if (j == 1) throw j;
return j;
}
var i;
for (i = 0; i < 3; i ++) { let r; try { r = throw1 (i); } catch {}; console.log (r); } // Case A: 0, undefined, 2
for (i = 0; i < 3; i ++) { var r; try { r = throw1 (i); } catch {}; console.log (r); } // Case B: 0, 0, 2
for (i = 0; i < 3; i ++) { var r = undefined; try { r = throw1 (i); } catch {}; console.log (r); } // CAse C: 0, undefined, 2
// i. catch uninitialised variables (case A or B)
// ii. catch use of var uninitialised (case B)
// iii. catch use of var rather than let (case B or C)
New contributor
Owner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.