1

Consider the following code:

const testArray = ['string1', 'string2', 'string3'];

for (let iterator in testArray) {
    console.log(iterator == 0);
}

My IDE (Visual Studio Code), complains about the console.log, that it will always return false (error: ts(2367))

But if I input this code directly either here, or in a JSBin, or in my current browser, I always get the true result on the first iteration. Anyone can explain me, if my IDE is to blame for this, or maybe there is some legacy javascript, where that condition indeed always returns false?

Based on this table https://dorey.github.io/JavaScript-Equality-Table/ it also seems that the above code is valid, and will run as expected.

const testArray = ['string1', 'string2', 'string3'];

for (let iterator in testArray) {
    console.log(iterator == 0);
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • Perhaps it means that the function call to `console.log()` always returns `false` (which is not quite true; it always returns `undefined`). – Pointy Jul 12 '20 at 14:00
  • 2
    Also are you working with Typescript? That very well could be the issue, as the `iterator` values will always be strings, but your `==` comparison is to a number. (That error message is a Typescript error so ...) – Pointy Jul 12 '20 at 14:02
  • In the code in which I discovered this, I indeed use typescript, but afterwards, I made a separate JS file, with only this content, and `jsconfig.json` with `checkJS` true, and it still complains. Btw, if I change that line to: `if (iterator == 0) {}` it still complains, and it explicitly says that the `condition` will always be false. – Adam Baranyai Jul 12 '20 at 14:03
  • @AdamBaranyai Maybe your typescript had `===` instead of `==`? – Bergi Jul 12 '20 at 14:05
  • I don't know much about VSCode but if it's giving you a Typescript error, it's confused about the nature of your code. – Pointy Jul 12 '20 at 14:05
  • @Bergi My TypeScript code also had `==` – Adam Baranyai Jul 12 '20 at 14:06
  • @AdamBaranyai what if you try `iterator == "0"`? – Pointy Jul 12 '20 at 14:06
  • @Pointy VSCode states, that it can also type check javascript code with JSDoc, it seems that's not the case maybe? If I compare the `string` to `string`, the complaint dissapears. But it feels hacky to write it like that... – Adam Baranyai Jul 12 '20 at 14:06
  • Sorry I really know essentially nothing about it or how it works. – Pointy Jul 12 '20 at 14:07
  • @Bergi I suspect there really is a duplicate; did I miss some answer in that question that directly addressed this? Of course I think `for ... in` used this way is a bad idea but the VSCode behavior is I think the key part of it. – Pointy Jul 12 '20 at 14:08
  • @Pointy why it is considered a bad idea? I mean, in most cases, where the array that is being iterated over doesn't change that much, it is much faster to type it this way, then with `for (let i = 0; i – Adam Baranyai Jul 12 '20 at 14:11
  • 1
    https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea — there's a lot of information there. It used to cause more problems like 10 or 15 years ago when more libraries added properties to system prototype objects, so they'd show up unexpectedly in your `for ... in` loop. – Pointy Jul 12 '20 at 14:14
  • @Pointy And it still causes problems today by having strings as property names instead of fast-path integer indices. – Bergi Jul 12 '20 at 14:15
  • @Bergi yes and you're right, a couple of the answers on that old bug explicitly mention that, even though probably most of them pre-date Typescript by a few years. – Pointy Jul 12 '20 at 14:16
  • 2
    @AdamBaranyai If you want a shorthand for iterating arrays, `for … of` is the way to go. – Bergi Jul 12 '20 at 14:16
  • 1
    Oh also [see this question, in particular the answer that starts with **short version**](https://stackoverflow.com/questions/57125700/why-use-triple-equal-in-typescript) – Pointy Jul 12 '20 at 14:27
  • @Bergi am I mistaken in that `for ... of` iterates over the values of an array, instead of it's `indexes`? In the example where I was using this, I was interested explicitly in the index of the array. – Adam Baranyai Jul 12 '20 at 14:28
  • @AdamBaranyai Then use [`for (const i of testArray.keys())`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys) – Bergi Jul 12 '20 at 15:29

0 Answers0