Hey everyone! ð
Today, while working on my journey as a computer science student, I was tackling a 2D Arrays challenge in JavaScript. Everything seemed perfect. The logic made sense, the loops looked solid... but the code just wouldn't work.
I spent about 20 minutes staring at the screen, running mental tests, and questioning my programming skills.
Can you spot the error in this snippet?
for(let j = 0; j < matrix.length ; j++){
let value = matrix[j][0];
for(let k = 0; k < matrix[j].lenght; k++){
// Logic here
}
}
Yeah... it was the 't' at the end of matrix[j].lenght. I wrote lenght instead of length.
The JavaScript Trap ðŠĪ
The worst part about this specific typo in JavaScript is that the console won't throw an explicit error. JS just looks at matrix[j].lenght, says "Well, that property doesn't exist, so it's undefined", and moves on.
Since k < undefined evaluates to false, the inner loop never executed, leaving me completely stuck.
My Lesson of the Day ðĄ
I felt a bit silly when I finally noticed that tiny mistake, but it reminded me of two valuable lessons:
Always check the typos first before assuming your entire logic is broken.
Use linters or spell checkers! (Time to install one in my editor ASAP).
If you are also learning to code, don't feel bad when these things happen. It's a classic rite of passage for every developer!
Have you ever lost hours of work because of a single misplaced letter? Let me know in the comments! ð
Follow my journey as I build in public! ð
Top comments (1)
The cruel part is that it didn't even throw an error â
undefinedjust quietly made your loop condition false and let you go hunting for a logic bug that never existed. Silent failures are so much meaner to beginners than loud ones. Mine from last week: I was convinced my emoji feature was broken, tore through the code twice, and the actual culprit was my terminal mangling the emoji before they ever reached the app. The bug is almost always somewhere dumber than where you're looking. Twenty minutes is honestly a respectable time-to-typo. Welcome to the club â the membership fee is exactly this post.