From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/statements/if...else/index.html | 176 +++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/statements/if...else/index.html (limited to 'files/zh-cn/web/javascript/reference/statements/if...else/index.html') diff --git a/files/zh-cn/web/javascript/reference/statements/if...else/index.html b/files/zh-cn/web/javascript/reference/statements/if...else/index.html new file mode 100644 index 0000000000..90d29e86ae --- /dev/null +++ b/files/zh-cn/web/javascript/reference/statements/if...else/index.html @@ -0,0 +1,176 @@ +--- +title: if...else +slug: Web/JavaScript/Reference/Statements/if...else +tags: + - JavaScript + - Statement +translation_of: Web/JavaScript/Reference/Statements/if...else +--- +
+
 {{jsSidebar("Statements")}}
+
+ +

当指定条件为真,if 语句会执行一段语句。如果条件为假,则执行另一段语句。

+ +

{{EmbedInteractiveExample("pages/js/statement-ifelse.html")}}

+ + + +

语法

+ +
if (condition)
+   statement1
+[else
+   statement2]
+
+ +
+
condition
+
值为真或假的表达式
+
+ +
+
statement1
+
condition为真时执行的语句。可为任意语句,包括更深层的内部if语句。要执行多条语句,使用语句({ ... })将这些语句分组;若不想执行语句,则使用语句。 
+
+ +
+
statement2
+
如果condition为假且 else从句存在时执行的语句。可为任意语句,包括块语句和嵌套的if语句
+
+ +

说明

+ +

多层 if...else 语句可使用 else if 从句。注意:在 Javascript 中没有 elseif (一个单词)关键字。

+ +
if (condition1)
+   statement1
+else if (condition2)
+   statement2
+else if (condition3)
+   statement3
+...
+else
+   statementN
+
+
+ +

要看看它如何工作,可以调整下嵌套的缩进:

+ +
if (condition1)
+   statement1
+else
+   if (condition2)
+      statement2
+   else
+      if (condition3)
+...
+
+ +

要在一个从句中执行多条语句,可使用语句块({ ... })。通常情况下,一直使用语句块是个好习惯,特别是在涉及嵌套if语句的代码中:

+ +
if (condition) {
+   statements1
+} else {
+   statements2
+}
+
+ +

不要将原始布尔值的truefalseBoolean对象的真或假混淆。任何一个值,只要它不是 undefinednull、 0NaN或空字符串(""),那么无论是任何对象,即使是值为假的Boolean对象,在条件语句中都为真。例如:

+ +
var b = new Boolean(false);
+if (b) //表达式的值为true
+
+ +

示例

+ +

使用 if...else

+ +
if (cipher_char === from_char) {
+   result = result + to_char;
+   x++;
+} else {
+   result = result + clear_char;
+}
+
+ +

使用 else if

+ +

注意,Javascript中没有elseif语句。但可以使用elseif中间有空格的语句:

+ +
if (x > 5) {
+ /* do the right thing */
+} else if (x > 50) {
+ /* do the right thing */
+} else {
+ /* do the right thing */
+}
+ +

条件表达式中的赋值运算

+ +

建议不要在条件表达式中单纯的使用赋值运算,因为粗看下赋值运算的代码很容易让人误认为是等性比较。比如,不要使用下面示例的代码:

+ +
if (x = y) {
+   /* do the right thing */
+}
+
+ +

如果你需要在条件表达式中使用赋值运算,用圆括号包裹赋值运算。例如:

+ +
if ((x = y)) {
+   /* do the right thing */
+}
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-if-statement', 'if statement')}}{{Spec2('ESDraft')}} 
{{SpecName('ES6', '#sec-if-statement', 'if statement')}}{{Spec2('ES6')}} 
{{SpecName('ES5.1', '#sec-12.5', 'if statement')}}{{Spec2('ES5.1')}} 
{{SpecName('ES3', '#sec-12.5', 'if statement')}}{{Spec2('ES3')}} 
{{SpecName('ES1', '#sec-12.5', 'if statement')}}{{Spec2('ES1')}}Initial definition
+ +

浏览器兼容

+ + + +

{{Compat("javascript.statements.if_else")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf