JavaScript Basics: Why no semicolon?

general question:

ex.:
const shoutGreetings = arr => arr.map(word => word.toUpperCase() + '!');

Why is there no semicolon needed directly after .toUpperCase()? Shouldn’t there be one, since this is a method call?

When there is no statement following, semi-colon is not needed.

1 Like

The ; marks the end of a statement. If you did place a ; after the .toUpperCase() method, you would raise an error. The statement would end with the ;, so that line would have 2 statements. The first one would be: const shoutGreetings = arr => arr.map(word => word.toUpperCase(); and the second would be: + '!');. The first statement is missing the closing ) for the .map() method. The second statement has a closing ) without an opening (. The semi-colons mark the end of statements not method calls. As @mtf pointed out, semi-colons are not needed following ) if no additional statement follows on the same line of code. Consider this example:

const hello = "Hello"
console.log(hello)
let count = 1
while (count < 5) {
  console.log(count)
  count++
}
console.log("I'm finished, and didn't use a single semi-colon.")

Output:

Hello
1
2
3
4
I’m finished, and didn’t use a single semi-colon.

While they aren’t necessary much of the time, it’s good practice to use them at the end of statements, for readability of humans. If you were to combine several statements on one line they become necessary for the interpreter to tell where each statement ends. Consider this example:

const hello = "Hello";console.log(hello);let count = 1;while (count < 5){console.log(count);count++}console.log("I'm finished, and only used one line of code.")

Output:

Hello
1
2
3
4
I’m finished, and only used one line of code.

While this example produces the desired output, it’s not very human reader friendly. If any of the semi-colons between statements were missing we’d get a SyntaxError: Unexpected identifier
In summary, semi-colons aren’t required very often, but enhance readability.

2 Likes

thanks so much! appreciate your details.

1 Like

Not all statements end in semi-colon. Some examples: for, if, {}

In what case does semi-colon enhance readability?

We can add arbitrary amounts of semi-colons at the end of statements. How about 5? No?
Why would you want fewer? Oh because they don’t do anything and they’re visual clutter? Well, let’s remove as many as possible then. 0. If you say 1 I’ll be able to use the same argument for 2, 3, and so on. Need a delimiter? The line ended, there’s nothing on the other side of the would-be delimiter. Need a statement terminator? The line ending does that too.

Python has optional semi-colons at ends of statements. They don’t see much use, not even to make things easier to read!

I didn’t write the Codecademy curriculum on JavaScript, and it uses semi-colons at the end of pretty much every statement. To enhance readability is the only reasonable argument I could come up with for using them. Much like periods in this paragraph. Without the periods, you could most likely still tell where each sentence ends, but the periods leave no doubt. Seemed wrong to tell a learner to never use semi-colons when the curriculum shows them all the time, and the SCT would probably fail code without them.