JavaScript if...Else 语句

Javascript 教程 · 2019-03-02 15:57:26

条件语句用于基于不同的条件来执行不同的动作。

条件语句

通常在写代码时,您总是需要为不同的决定来执行不同的动作。您可以在代码中使用条件语句来完成该任务。

在 JavaScript 中,我们可使用以下条件语句:

  • if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码
  • if...else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码
  • if...else if....else 语句- 使用该语句来选择多个代码块之一来执行
  • switch 语句 - 使用该语句来选择多个代码块之一来执行

if 语句

只有当指定条件为 true 时,该语句才会执行代码。

语法

if (condition)
{
    当条件为 true 时执行的代码
}

请使用小写的 if。使用大写字母(IF)会生成 JavaScript 错误!

实例

当时间小于 20:00 时,生成问候 "Good day":

if (time<20) { x="Good day"; }

x 的结果是:

请注意,在这个语法中,没有 ..else..。您已经告诉浏览器只有在指定条件为 true 时才执行代码。

if...else 语句

请使用 if....else 语句在条件为 true 时执行代码,在条件为 false 时执行其他代码。

语法

if (condition)
{
    当条件为 true 时执行的代码
}
else
{
    当条件不为 true 时执行的代码
}

实例

当时间小于 20:00 时,生成问候 "Good day",否则生成问候 "Good evening"。

if (time<20) { x="Good day"; } else { x="Good evening"; }

x 的结果是:

if...else if...else 语句

使用 if....else if...else 语句来选择多个代码块之一来执行。

语法

if (condition1)
{
    当条件 1 为 true 时执行的代码 }
else if (condition2) {
    当条件 2 为 true 时执行的代码 }
else {
  当条件 1 和 条件 2 都不为 true 时执行的代码 }

实例

如果时间小于 10:00,则生成问候 "Good morning",如果时间大于 10:00 小于 20:00,则生成问候 "Good day",否则生成问候 "Good evening":

if (time<10) { document.write("<b>早上好</b>"); } else if (time>=10 && time<16) { document.write("<b>今天好</b>"); } else { document.write("<b>晚上好!</b>"); }

x 的结果是:


点击查看所有 Javascript 教程 文章: https://www.codercto.com/courses/l/27.html

查看所有标签

算法:C语言实现

算法:C语言实现

塞奇威克 / 机械工业出版社 / 2006-9 / 69.00元

本书是Sedgewick彻底修订和重写的C算法系列的第一本。全书分为四部分,共16章,第一部分“基础知识”(第1-2章)介绍基本算法分析原理。第二部分“数据结构”(第3-5章)讲解算法分析中必须掌握的数据结构知识,主要包括基本数据结构,抽象数据结构,递归和树。一起来看看 《算法:C语言实现》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具