When to use semicolons?

Hello guys this is my first post ever here. i recently joined codecademy Pro and started learning introduction to java script. Overall the course is awesome and the instant interactivity and projects makes learning more enjoyable , i have covered 80% of the course but at around the modules section i started facing an issue whenever the task would not complete i would look at my code and would waste a lot of time trying to find the issue even though i would know that my code was correct but whenever i would look for the hints issue would be that i was missing a semicolon most of the time , i know that java script doesn’t care for semicolons most of the time but it seems that it cares about semicolon sometime i will be happy to know when to put semicolons and when not to put semicolons perhaps some one should write a post about it as there is no whatsoever explanation given in the course. i apologize in advance if you do not understand my question as English is not my first language but i tried hard to make my concern clear to understand. thank you

[https://www.codecademy.com/courses/introduction-to-javascript/lessons/modules/exercises/named-imports](link to exercise)

Hello @mtea994!! Welcome to the forums!! :grinning:

Though JavaScript will usually recognize when a line of code has ended it does not always.
As such it is best practice to always place them at the end of a line of code, so as to prevent bugs.

I will try to find a few examples of when JavaScript makes this mistake.

2 Likes

I found this on MDN:


Though it does not give specific examples of bugs, it does show why you should use semicolons.

JavaScript doesn’t make a mistake when interpreting the presence/lack of semi-colons, really. It’s a design feature, which is itself a mistake in the opinion of many people (myself included).

JS will attempt to correct for shoddy programming by inserting an implicit semi-colon where one ought to have been put by the programmer, according to a specific set of rules.

One such example of this behaviour is in this thread, alongside an explanation of the behaviour.

1 Like

when doing the exercises in javascript module i wouldn’t get any error in the console but the task wouldnt mark Complete i think they have made this to be specific in module section i don’t know why but they should have specified it int the start of the section…
and thanks for replying

Count me in on that. IMO JS should just throw a syntax error here.