While trying to solve one of Codecademys challenge projects, I’ve run into a challenge with if statements and checking whether a value is true or false in an array. What I expected to be a pretty simple line of code, seems to break the way JavaScript works. The problem is was as follows:
if (someArray[x][y] === ‘someValue’) { … }
And now this gets complicated. As I was writing the code yesterday, this gave me an error saying something along the lines of: “cannot read properties of undefined (reading ‘0’)” but when writing this blog post the day after, the error is gone. I am now able to access the array and check for values (I’ll be right back – refactoring entire codebase now…)
Where does this kind of experience bring me?
I think this kind of short “non-story” is typical of learning new programming languages. It’s an example of how your mental model of the language is challenged, and sometimes leads you to focus on the wrong problem. This is where it gets tricky, as you can spend a lot of time finding the right solution, and even more if you are trying to solve the wrong problem.
I’m glad I have some experience with Python and especially debugging, as I’m pretty sure the problem isn’t about accessing an array when evaluating if statements. Instead, I think it’s somewhere else in the code making the “someArray” or the “x” and “y” variables not storing the data I thought they did.
I’ve have been working with the developer tools in the browser but haven’t figured out how to debug in Node.js yet. What are your preferred way of debugging in Node.js?