Do you always add a `;` after a closing `}`?

I’m just wondering if you always add a ; after a closing }?

It seems like sometimes I see the ; after the } in code blocks etc and sometimes I don’t. Should I just always add a ;?

1 Like

no you don’t do use them alway’s into JS but they are very common

1 Like

The semicolon operator is used to terminate a statement. If you use new lines, the JS parser will automatically add them when it parses your code.

let val1 = 1
const val2 = 2
const add = (a, b) => a + b
function multiply (a, b) {
  return a * b
}
val1++

Which gets parsed as

let val1 = 1;
const val2 = 2;
const add = (a, b) => a + b;
function multiply (a, b) {
  return a * b;
};
val1++;

But if you put them on one line, you need them:

let val1=1; const val2 = 2; const add = (a, b) => a + b; function multiply (a, b) { return a + b }; val1++;

If they weren’t there then the line would be:

const val1=1 const val2 = 2 const add = (a, b) => a + b function multiply (a, b) { return a + b } val1++

Which wouldn’t make any syntactic sense, JS would add the semicolon at the end of the line only and the program would blow up.

Normally you can elide semicolons if you want to, as long as you make sure things go on seperate lines (I would assume you’d probably not write a line of code like the above, it’s almost impossible to read).

There is one place where the way JS automatically adds semicolons always causes an issue – it’s very easy to avoid it ever happening but here is what it looks like:

function badAdd (a, b) {
  return
    a + b
}

Because there is a new line after return, JS will automatically add a semicolon after it, so it will look like this:

function badAdd (a, b) {
  return; // function exits here
  a + b;
}

Anyway, there are lots of things written about it, Google for “js automatic semicolon insertion” or “js asi” or “js semicolons”, eg:

http://www.bradoncode.com/blog/2015/08/26/javascript-semi-colon-insertion/

I would say always use semicolons, or (much easier) just be aware that it can cause issues very occasionally and just use a code formatter (ie Prettier) to save yourself any headaches.

3 Likes

I personally don’t use semicolons in JS, but if a reformatter adds them, so be it. It’s nice how so many bikeshed arguments can be solved by using automated tools.

2 Likes

Yeah, as far as I can see, the '“accidentally putting the return value on the next line after return” is about the only time it’s ever an issue, so tbh not using them ever is generally fine, I think it’s just ingrained habit that I always try to remember to add them.

(Also, afaik the FCC challenge where you swap two variables using destructuring fails if you miss the semicolons out; I can’t quite remember the exact situation where that happens, but it definitely didn’t error last time I tried, it just did something weird due to ASI, so sometimes does have a direct effect)

1 Like

One correction: semicolon is used to terminate one statement. Block of (multiple) statements, such as function, class, if etc. do not separated with semicolon. But, when you’re assigning function to a variable that’s one statement - semicolons at the end:

function semicolon() {
  return ';'; // single statement with semicolon
}
// block of statements - no semicolon

const semicolon = function() {
  return ';'; // single statement with semicolon
};
// single statement with semicolon
1 Like

To add to the reply of @DanCouper and expand on what @snigo stated, aside from the automatic insertion on the return the only other place it will cause problems is if you have something like the code below

const whatever = function() {
  return 'yey'
} // Missing ";" here

(function() {
  console.log('this is an IIFE')
})()

What happens is that due to the lack of the separator ; it will be interpreted like this

const whatever = function example() {
  return 'something'
}(function() {
  console.log('this is an IIFE')
})()

Unless instead of returning the string something on the function example we return a function this will lead to an error. Even if a function it won’t behave as expected if we put the ;.

Since I can count on one hand the number of times this was a problem for me I neglect the semicolons but for paranoia’s sake, I let prettier or something similar format the code to add it when I save a file.

Regardless if you decide to use them keep them throughout all the code for consistency, same if you decide not to use them.

2 Likes

Ah, that’s what the issue was in the FCC challenge, I remember now, it initialises two variables, then there’s an anonymous function with the destructuring assignment inside, so without the semicolon it fails in the way you describe

2 Likes