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
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.
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
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.