JavaScript Semicolon Usage Summary

Source: Internet
Author: User

No should not, only you like not to like. The fact that JavaScript syntax is c-like does not mean that it is essentially a class of languages, and that all intuitive "of course, semicolons" are conservative and hasty conclusions without deep thought. Later the new design of the language can be more than the choice of semicolons, just "can be added semicolon but everyone does not add" the language is: Go, Scala, Ruby, Python, Swift, Groovy ...
As for saying "it is difficult to sum up when to add", it is really very simple indeed. There are 5 tokens that really lead up and down the parsing problem: brackets (), square brackets [], regular start slash/, plus +, minus-. I have never seen the actual code with a regular, plus, minus sign as the beginning of the case, so summed up is a sentence: a line begins with parentheses or square brackets when the semicolon can be, all other times do not need. In fact, even in these two cases, it is quite rare in the actual code.
In addition, restricted production this thing (that is, cause return after the change line automatically inserts the semicolon mechanism), whether you add the semicolon you have to understand to not be the pit, and add the semicolon does not matter.
More details can be seen I once gave a talk:hacking semicolons by Evan you
Finally, the point code is good, vue.js code all without semicolons: Yyx990803/vue GitHub
In addition to the tool, I did write a, automatic help you batch add or remove the semicolon: Yyx990803/semi Make a Git pre-commit hook on GitHub and choose the style you like.

Summarize:

1. Write your own code: A complete statement with a semicolon, function declaration without semicolons

1.  Add Semicolon statement var jason = "Zeng"; var eason = function () {      //other statement ...};(function ($) {      //other statement ...}) (jQuery); ++a;b++;//2. Declaration without a semicolon function myfunction () {      //other statement ...}

2. When combined with someone else's code: when you find that others have features without semicolons, you add a semicolon in front of the statement

;(function ($) {     //other statement ...}) (JQuery); ++a;

3. Return cannot be single-line exclusive

var G1 = function () {     return ' test ';  } var g2 = function () {     return {        a:1,        b:2    }  }var g3 = function () {     return [         "one",         "Both"     ];}        

JavaScript Semicolon Usage Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.