为什么在 if/else 语句之后不使用分号? [英] Why are semicolons not used after if/else statements?

查看:55
本文介绍了为什么在 if/else 语句之后不使用分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Javascript 中的所有语句之后使用分号是一种很好的语法,但是有人知道为什么 if/else 语句在大括号之后不需要它们吗?

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 braces?

推荐答案

  • 分号用于结束 ONE 语句
  • {} 开始和关闭一组语句
    • Semicolon is used to end ONE statement
    • { and } begin and close a group of statements
    • 基本上,if-else 后面必须跟一个语句或一组语句.

      Basically, an if-else must be followed by either a statement or a group of statements.

      if-else 后跟一个语句:

      if (condition) statement;
      if (condition); // followed by a statement (an empty statement)
      

      if-else 后跟一组语句:

      if (condition) {
         statement;
         statement;
      }
      
      if (condition) {
         // followed by a group of statements of zero length
      }
      

      if-else 必须以 ; 结尾,如果它后跟单个语句.if-else 后面跟着一组语句时不以 ; 结尾,因为 ; 用于结束单个语句,而不是用于结束一组语句.

      if-else must end with a ; if it is followed by a single statement. if-else does not end with a ; when followed by a group of statements because ; is used to end a single statement, and is not used for ending a group of statements.

      这篇关于为什么在 if/else 语句之后不使用分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆