A side note, and one I repeat a LOT, is the use of comments. I firmly believe that adding comments to your code can only help, for a number of reasons. I was just reviewing a js file I’d written up a couple weeks back for the calculator challenge – the js file is 191 lines, of which only 88 are actual code. The rest is comments (and empty lines).
First, if I comment the code with the intent behind that code, then when I write the code, I can compare what actually happens with what I expected. Often, I will write the entire program/function in commented ‘pseudo-code’, explaining the logic in plain english as completely as needed. Then, when I actually build the thing, I will leave the comments in place, either at the opening of the file, or as a line-by-line explanation. It helps me keep track of the logic, each step of the way.
Second, if someone is checking out your code later, they can see your logic. They can get a handle on what your variable names may mean, or what your expectations are, or if you are using some truly advanced and funky stuff, you can help them learn. I remember the first time I encountered a
.map(..) that was commented beautifully, learned more from seeing the actual output AND the comments.
Third, sometimes I am on my ‘A game’. Not always. There are days I revisit my own code, and have to wonder “Exactly how drunk was I that night…?” It may make sense when I’m in the zone, but on my average day, I can confuse the heck out of myself. Thus, comments.
I do agree with the index comment, though – remember that arrays, by their nature, are indexed. element
0 in an array will remain element
0, unless you explicitly rearrange things. Objects are not that way, there is no guarantee that object properties will return in any given order, but arrays, and array indexes, are fundamentally linked.
And that calculator js page? Here’s the link, and it uses jQuery pretty extensively. Not because I needed to, I’d originally written it in pure JS, then in React, then in jQuery, simply to compare and contrast. But this one was heavily commented, as it was being used as a tutorial piece.