Automatic detection of wrong index typos

let numbers = Array.from({length:100}, (_,i)=>i);
for ( let i = 0; i < 10; i++ ) {
  for (let j=0;j < 20; j++ ) {
    let number = numbers [ i ];
  }
}

This is the place where the compiler, interpreter or IDE could publish an error or a warning.

This is a clear situaion when a programmer wanted to use the key "j" but instead made a typo and used the key "i". Had he wanted to use the "i" key he would have initiated the variable "number" inside the first loop, not the second one.

In my opinion, future IDEs, compilers and interpreters could fixate such obvious errors and point them out to a programmer. Those bugs are often pretty hard to find.

What exactly makes it clear that the "j" variable was intended? The best thing I can think of is the fact that "j" is currently unused. You can configure a linter that automatically notifies you if you have unused variables. I'm personally not a fan of that kind of warning (too many false positives), but I see the value in it.

3 Likes