Home > Web Front-end > JS Tutorial > body text

Do I need to add semicolons to JS code?

Guanhui
Release: 2020-06-03 09:20:18
forward
2366 people have browsed it

Do I need to add semicolons to JS code?

Semicolons in JavaScript delineate communities. Some people prefer to use semicolons anyway. Others prefer not to add a semicolon.

After many years of using semicolons, in the fall of 2017 I decided to try not adding semicolons as needed, and set up Prettier to automatically remove semicolons from my code unless necessary code structure required them.

Now I find that not using semicolons is very natural, I think such codes look better, they are more concise and readable.

This is entirely possible because JavaScript does not strictly require semicolons. When a semicolon is needed somewhere, it adds it behind the scenes.

This process is called automatic semicolon insertion.

It is important to understand the rules for using semicolons to avoid writing buggy code that behaves differently than you expect .

JavaScript rules for automatically adding semicolons

The JavaScript interpreter will automatically add semicolons when it finds the following special circumstances when interpreting source code:

  • When the next line of code begins to interrupt the current line of code (the code can be written in multiple lines)

  • When the next line begins with }, close the current block

  • When the end of the source code is reached

  • When return is declared on the current line

  • When break is declared on the current line

  • When throw is declared on the current line

  • When continue is declared on the current line

With your Code Examples

Think differently about these rules, here are some examples.

Look at the example:

const hey = 'hey'
const you = 'hey'
const heyYou = hey + ' ' + you
['h', 'e', 'y'].forEach((letter) => console.log(letter))
Copy after login

You will get the error Uncaught TypeError: Cannot read property 'forEach' of undefined because based on rule 1, JavaScript will try to interpret the code as

const hey = 'hey';
const you = 'hey';
const heyYou = hey + ' ' + you['h', 'e', 'y'].forEach((letter) => console.log(letter))
Copy after login

This code:

(1 + 2).toString()
Copy after login

prints as "3".

const a = 1
const b = 2
const c = a + b
(a + b).toString()
Copy after login

Instead, a TypeError: b is not a function exception is raised because JavaScript tries to interpret it as

const a = 1
const b = 2
const c = a + b(a + b).toString()
Copy after login

Another example based on Rule 4:

(() => {
  return
  {
    color: 'white'
  }
})()
Copy after login

You expect the return value of this immediately called function to be an object containing a color property, but this is not the case. Instead, it's undefined because JavaScript inserts a semicolon after return.

Instead, you should put the left bracket after return:

(() => {
  return {
    color: 'white'
  }
})()
Copy after login

You think this code will display '0':

1 + 1
-1 + 1 === 0 ? alert(0) : alert(2)
Copy after login

Instead it will display 2, because JavaScript according to Rule 1 will be interpreted as:

1 + 1 -1 + 1 === 0 ? alert(0) : alert(2)
Copy after login

Ending Statement

Be careful. Some people have a problem with semicolons. I don't really care, this tool gives us an option to not use it, so we can avoid using semicolons.

I'm not suggesting anything, I'm just letting you make your own decision.

We just need to note that even though most of the time these basic scenarios never appear in your code.

Excerpt some rules, as follows:

  • Use return statements carefully. If you return something, add it to the same line as the returned content (similar to break, throw, continue)

  • Never start a line with parentheses, these may Concatenated with the previous line to form a function call or array element reference

Finally, always test your code to make sure it meets your needs.

Recommended tutorial: "JS Tutorial"

The above is the detailed content of Do I need to add semicolons to JS code?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!