Semicolon


#1

I have some struggles with the semicolon. I don't know when to put it after something. I know that you have to put a semicolon sometimes behind a closing parameter or a closing bracket but I don't know when to put it and why. Can someone explain this too me.


I have to celebrate you baby, error
#2

Here is an excellent post on the topic :slight_smile:

https://www.codecademy.com/blog/78


#3

I know I have read it but still don't understand :stuck_out_tongue: :frowning:


#4

OK. Let's take this as a reference and walk it through.

Which is the first line/paragraph that is confusing?


#5

var quarter = function(number) {
return number / 4;
};

if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}

This for example


#6

heh..You took up my offer, wasn't expecting that :slight_smile:
I am tired and can't recall all the theory but I'll give a try.

So there is a semicolon after return number / 4; because a semicolon is for JavaScript interpreter like a stop sign. At the moment you are telling it to divide number by 4, but if you would not kill it (with the stop sign/ semicolon) it would could go on snooping, looking if it could add/subtract or do anything else...
Luckily .js interpreter is quite perceptive and forgiving and if you don't put a semicolon after return number / 4; it will not punish you.

You can sometimes get away leaving out semicolons. Like the semicolon after the closing bracket };, because there is a line break. But I personally believe "better to be safe" and so with the semicolon you are telling the js interpreter excplicitly to pull the brakes, that this is the end of the statement.

console.log("The statement is true"); again, here yo could leave out the semicolons after the console.log method, because nothing else follows. But the code you are building in the future will not be so simple and imo strong base techniques are important. https://www.youtube.com/watch?v=R37pbIySnjg

If you would put a semicolon after this if (quarter(12) % 3 === 0 ) and before an openig bracket you would kill it, remember a big stop sign.


#7

You're amazing ty :smiley: