🎙️
22

Wasted 3 days on a missing semicolon in JavaScript

I was debugging a simple calculator app and could not figure out why the submit button did nothing. Turns out I forgot a semicolon after a function call, something every guide says is optional but broke my whole script. Has anyone else spent way too long on a tiny typo like this?
2 comments

Log in to join the discussion

Log In
2 Comments
the_eric
the_eric26d ago
That semicolon thing is a trap everyone falls into at least once. The optional semicolon thing only works if you put it at the end of the line you're breaking, not in the middle of a statement that relies on automatic semicolon insertion. Your function call probably got parsed as part of the next line, so your button handler never actually ran. A good habit is to always use semicolons even if they seem optional. Saves you from these 3 day headaches. I also started using a linter that flags missing semicolons as an error. Seems harsh but it catches this exact problem before you even run the code.
3
viola_ward
viola_ward25d ago
Read a blog post that said the same thing, @the_eric. Semicolons prevent weird bugs.
5