search

Home  >  Q&A  >  body text

javascript - Regarding the issue of semicolon, why is an error reported without a semicolon?

Output 2

When the semicolon is removed, an error is reported

May I ask what the semicolon does? Why does removing it result in an error?

高洛峰高洛峰2757 days ago597

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:44:00

    It’s not a semicolon problem,
    but if there is no blocking of ; sign, the interpreter directly merges the two statements into:

    var xx = function(){}[1,2,3].aa(22);

    Pay special attention when using statements starting with () and [], because these two operators will be combined with the previous expression first, and the ; sign cannot be omitted

    In some cases; sign is not necessary, such as;

    var a = 1, b=2
    a++
    b++

    This will not report an error

    Since js does not clearly explain such behavior, and the page is easy to be confused, the recommended approach is generally:
    Whether it is necessary or not, you'd better add a sentence at the end of the ";" separator, unless you Know exactly what the result is (want).
    This is called best practice

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:44:00

    function(){}[1,2,3] returns undefined

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:44:00

    The semicolon here should indicate the end of this statement, right?

    Array.prototype.aa = function(){console.log(2);};

    I think you can tell it when you write it like this.

    reply
    0
  • Cancelreply