Idiot question: when to use ';' and when not

I’m struggling with the use of ‘;’.

I realize how it is used with ‘if’-statements, but the rest…?

Be patient, please, and try to explain this example:

// AJAX functions
const shortenUrl = () => {
  const urlToShorten = inputField.value;
  const data = JSON.stringify({destination: urlToShorten});
  fetch(url, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'apikey': apiKey
    body: data
  }).then(response => {
    if (response.ok) {
      return response.json();
    if (!response.ok) {
      throw new Error('Request failed!')
  }, networkError => console.log(networkError.message)
         ).then(jsonResponse => {

MDN only say “each command line should close with semicolon”.

So, why do I close a “single” console.log line with semicolon, but NOT the console.log of the arrow function?
Should there be a semicolon after the end bracket of the last .then() or not?
Should the whole shortenUrl-function close with }, ?

Do you know where this is explained with examples?

Thanks a lot!

Some statements have a semicolon in them.
So if you wonder, look at the syntax for that statement.
For example:

You might want to use some linter to check your code style, I think you’d learn quickly by reading and fixing the things it points out.

It can also be argued that since automatic semi-colon insertion causes most semi-colons to have no effect, they don’t belong there.
IMO this “feature” of making them optional is a mistake because it makes it very difficult to figure out where they’re actually supposed to go. Pick a style and stick with it, doesn’t matter which as long as it’s consistent.

@ionatan :
Thanks a lot, again.

Ongoing idiot (rookie) question: All I do is follow the course on codecadamy. I only type in the browser based ‘main.js’.

So, while of course I have a browser, I have no specific editor, IDE, least a linter (I had to look up what that is and the dictionary said it’s some cotton…) -

What can you suggest, please?
Atom + IDE + linter ?

A linter is a program that reads code and looks for… whatever issues it has been made to detect

I say go with a programmable editor like atom/vscode/vim/emacs, and don’t make it responsible for running your code, it’s not an operating system it’s an editor. (Some new programmers are very quick to rush to the conclusion that all activities should happen inside some kind of development program… the reality is that that program is your operating system, or even multiple computers)

1 Like

Thanks! I’ll have a look into atom.

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