Semi colon not needed?

Tell us what’s happening:
So I understand what is going on. I have passed this part of the curriculum. I really just have a question. My question is about the semi colon on this specific string. The test passes with or without the semi colon. Why is that? How come I dont need the semi colon here. Is there a reason? Thats honestly the question i’m wondering about.
Your code so far


// Only change code below this line

var myStr ="This is the first sentence. "
myStr += "This is the second sentence."

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Concatenating Strings with the Plus Equals Operator

Link to the challenge:

There are different kinds of semicolons in JS. The end of the line semicolons that are nor required in a sense because JS will add them in automatically for you while compiling, behind the scenes. There are debates over whether or not to use them - some people always add them in, others are adamant to leave them out. There are a few edge cases where it can make a difference, but very few.

There are also semicolons that must be there, like in a for loop. Those are not optional.

2 Likes

There are places where you need a semi colon and there are places where you don’t need it. I don’t know for sure but to make sure you never miss one that is needed, it is good practice to always add one.

1 Like

the difference is that in previous challenges the tests were checking if the semicolon was there, as the challenges become more complex it is difficult to test for small things like that, it would make the challenge to restrictive, so it’s left to you to add them

Good idea. I’m assuming its never a wrong move to always include the semi colon like you say then?

1 Like

Do you agree to always include the semi colon for good practice?

it is. It is not necessary for a thing called Automatic Semicolon Insertion, but it is good practice to add semicolons

3 Likes

Ok thank you so much for specifying. I will probably just always incorporate semi colon for best practice just to steer away from debatable uses.

Got it. I will definitely take your word for it thank you! :slight_smile:

Yeah, there are definitely heated sides to this debate. I used to work with a team that was adamantly anti-semicolon. And I’ve worked on teams where they were required (enforced by linters). My personal feeling is that they do add a tiny bit of clarity to the code and that they do prevent a few edge-case gotchas. I use semicolons.

2 Likes

I usually just let Prettier do its thing. Personally prefer reading code with semicolons.

Blog post
https://2ality.com/2011/05/semicolon-insertion.html

Specs
https://tc39.es/ecma262/#sec-rules-of-automatic-semicolon-insertion

1 Like

Wow, thanks for clearing that up. I know it seems trivial to ask a question like this but im super ocd about directions lol. From now on I WILL be using semicolons.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.