Basic Javascript semicolon question

are there any general rules of thumb for when to use a semicolon? often I find myself screwing up code by forgetting a closing bracket, but when it comes to if else statements, functions and such I sometimes wonder:

“why does a semicolon go here?”
“why does this work without a semicolon?”
“why don’t i need a semicolon?”

ive only gotten 95% through the javascript course on this website, though ive fooled around with other websites too, but the more i do this the more i ask myself this question

A semicolon ends a statement. Except… you don’t have to use them. Except… you should :slight_smile:


There are some edge cases where they are required to avoid errors and unexpected results.

One example, I like to use is the following when using an IIFE

const myVar = 'some value'


The above code result in:

TypeError: "some value" is not a function

The opening ( is the culprit. Adding a semi-colon after the first line allows the code to execute with no errors.


Is it just me or is there a growing trend not to use semicolons? I guess I’m not one of the cool kids because I always use them.

You don’t have them in Python, so maybe it’s Python kids coming over and bringing their new-fangled ways to JS?

I also read a blog post by some guy who said he wants to reduce wear and tear on his fingers, which I can get…but then I was like, nah, I’m going to write semicolons.

I may start using ’ instead of ", though. Maybe…

I like the semicolon, but I’m a belt and suspenders sort of guy. I find explicit clarity to be best in code.

JavaScript is pretty lenient when it comes to the usage of semi-colons.

MDN definition on semi-colons:

In JavaScript, instructions are called statements and are separated by semicolons (;).

So at this point you might be asking “what the heck are instructions or statements”? I’d say go read over the docs or just don’t worry about the definitions, and focus on the syntax. Simply because if you memorize the rules/syntax you’ll understand the definition more than if you learn the definition and try to learn the rules/syntax from it.

Generally, as said above you don’t actually need ; for JS to work, this is due to most environments running your code assuming what your doing, however there are always edge cases so I’d be careful about knowing about them, or knowing where ; go.

Long story short, explicit placement of ; goes where it needs to go for your code to work. Its hard to pin down a “rule of thumb” the same way its hard to pin down what “the right code is”, because there are infinite possibilities to do something, and infinite possibilities to do it wrong ;D

Good luck, keep learning, keep buildings :smiley:

randel i need help with my work

Dear Randell,
Please could you hep me with my school work in computer science because I am stuck and I really need your help.


hi randel
i am a huge fan, i am stuck in computer science.
thank you in advance. :frowning:

help xx
var myVar = 11;

// Only change code below this line

myVar = myVar - 1;

i see you are a fellow deugon master. we can talk about that if you want. :grinning:

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like