Why are semicolons not used after if/else statements?

前端 未结 3 775
悲哀的现实
悲哀的现实 2020-12-08 06:59

I understand that it is good syntax to use semicolons after all statements in Javascript, but does any one know why if/else statements do not require them after the curly br

3条回答
  •  有刺的猬
    2020-12-08 07:51

    The real answer is because many modern languages copied their syntax from C, which has this property. JavaScript is one of these languages.

    C allows statement blocks

     { ... }
    

    (which don't need terminating semicolons) to be used where statements can be used. So you can use statement blocks as then- and else- clauses, without the semicolons.

    If you place a single statement in the then- or else- clause, you'll need to terminate it with a semicolon. Again, just as in C, with the extra JavaScript twist that ; is optional at the end of a line, if inserting it would not cause a syntax error.

提交回复
热议问题