Skip to content
Advertisement

Am I using semicolons right in js?

I have been doing some basic js but I am not sure if I am using semicolons correctly. Here is my code:

JavaScript

Please correct me in the comments if I am using them wrong.

Thanks, Reece C.

Advertisement

Answer

Modify the following lines and it’d look good to me, semi-colonwise.

JavaScript

The semi-colon is only required in JavaScript when two statements are on the same line, like this:

JavaScript

Therefore, the semi-colon can be happily omitted when statements are separated by a line break, like this:

JavaScript

However, ending each statement with a semi-colon may be considered a more disciplined approach (this way all statements will end the same way) and could help you avoid mysterious bugs later.

More information can be found here, here, and here. See also, this SO question.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement