My for loop printed 47 lines of "undefined" and I finally get why
I was sitting at my kitchen table in Tucson last Thursday working through a free JavaScript course, and I wrote a loop to print my grocery list but every single item came out as undefined. I stared at it for like 20 minutes before I realized I was using a variable name that didn't match the array I made, one little typo with a capital L. The console doesn't yell at you for stuff like that, it just quietly hands you garbage. Changed it, ran it, and all 6 items popped up perfectly. Does anyone have a trick for catching these naming slips before you waste half an hour on them?
That line about the console quietly handing you garbage is the truest thing I have read all week. It does not care that you meant well, it just gives you undefined and moves on with its day. My question is this: when you finally spotted the capital L, was it because you went back and read your own code line by line, or did you do something like drop a console.log on each variable to see which one was empty? I ask because the trick people always give is "just read it slower," and that never works for me. What actually works is printing the thing I think is broken, and nine times out of ten the variable is fine and the name I typed somewhere else is the problem.