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

查看:484
本文介绍了为什么在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?

推荐答案

  • 分号用于结束一个语句
  • {}开始并关闭一组语句
    • 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天全站免登陆