Is the semicolon needed at the end of JavaScript code?

The example has a semicolon but the image does not. I tried this exercise and it worked without one:

console.log('Hello, World!')


Is this necessary for JavaScript? If so, what does it do??


Link to content: Learn to Code with JavaScript - Section 1

You don’t, because the interpreter will attempt to insert one for you if don’t put one in (it’s called “automatic semicolon insertion”). However, it’s best practice to put them in yourself rather than relying on the interpreter to do it. We do note that you don’t need to use semicolons but you should here, but don’t mention that is due to automatic semicolon insertion. We’ll should probably updatethat in a subsequent edition. We do cover automatic semicon insertion in JavaScript: Novice to Ninja

1 Like

I still put a semicolon at the end and it used to be the case that if you used 'use strict'; that in the past the script would not run. However, that isn’t the case now as I have accidentally left out a semicolon and the script will still run now. However, I still would consider it good practice to put a semicolon at the end.

I prefer not to use semi-colons. I use standardJS to lint my code, which enforces some strict rules to formatting, but has a no-semicolons rule built-in. It will automatically remove them on save.

Code just looks cleaner to me without them, but if you are just starting out it maybe best practice to follow Simon_Mackie’s advice on this one and stick to using them.

Sometimes you will get caught out. For instance the following

const x = 2
[1,2,3].forEach((num) => console.log(num * x))

Will be interpretted as one line of continuous code

const x = 2[1,2,3].forEach((num) => console.log(num * x))

and will throw

Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')

Where as this will work

const x = 2;
[1,2,3].forEach((num) => console.log(num * x)); // 2,4,6
3 Likes

Semicolons as statement separators are standard since the beginning of software development. I do not know why everything, that has been has been proven for decades, must be changed only because someone thought he has an good idea.

1 Like

But if they add no purpose or benefit to the programmer (in 99.99% of lines of code), isn’t it better to remove redundancy and abstract away the unnecessary? The constant push to make coding easier and more efficient is the reason we’re not still all writing pure machine code into our terminals, despite it being a proven method.

Not really. In the days of punched cards, it would be one statement per card - the physical card was the separator.

3 Likes

Wouldn’t it be much more effective to remove the unit testing? Because 99% of it does never fail it looks not important for me :slight_smile:

1 Like

For me it’s about simplicity. “Always put a semicolon at the end of every statement” is an easy rule that’s not ambiguous in any way, whereas “well, in general don’t use it, unless the compiler can’t figure out that two lines are two statement” and all of a sudden I’m having to think for the compiler. It’s not an easy rule and it’s not clearly defined. You can be pretty certain that juniors will get bitten several times by this, wasting time figuring out why their code doesn’t work.
And for what? Aestetics? Is that really worth it?

8 Likes

I feel a bit silly now; :slightly_smiling_face:;

1 Like

Thats no problem. I guess this is more a requirement to become a software developer :wink:

If developers have trouble reading code due to exclusion of semi colons you have a hiring problem not a semi colon one. One can only imagine all the other inefficiencies that exist.

That’s not what I said at all …

Lets set aside “opinions” and let code speak for itself.

This works as intended WITH semicolons…

alert("Hello");
[1, 2].forEach(alert);

This does NOT work WITHOUT semicolons…

alert("Hello")
[1, 2].forEach(alert)

As @rpkamp so wisely pointed out, do you want to have to think for the compiler or let the compiler think for you?

5 Likes