9 ms·
I bounce between languages for different projects (work, home) and I have to look up examples for how to those JavaScript ones all the time.
by llanowarelves 4y ago
I bounce between languages for different projects (work, home) and I have to look up examples for how to those JavaScript ones all the time.
- seedboot 4y agoA great way to remember: Objects have keys -> foreign keys -> `for .. in`
- Etheryte 4y agoThe mnemonic I use is "for index", as in for keys you can use to index things. One gotcha to remember here is that officially all property names are strings [0], and that includes array indexes. To exemplify, `for (let x in { a: 40, b: 41, c: 42 }) { console.log(x); }` would give you "a", "b", "c" as that's what you can use as an index to key into that object to get the values. And `for (let x in [40, 41, 42]) { console.log(x); }` would give you "0", "1", "2" as that what's you can use as an index to key into that object. I always remember `for..of` as just the other one. [0] https://stackoverflow.com/q/27537677/1470607 https://stackoverflow.com/q/27537677/1470607
- masklinn 4y agoThe trick is to remember that “for in” is the obvious one, meaning it’s the older one, meaning it’s the one that’s next to useless and should almost never be used.