zoukankan      html  css  js  c++  java
  • JavaScript if...Else 语句

    JavaScript if...Else 语句


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

    条件语句

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

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

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

    if 语句

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

    语法

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

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

    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";
    }

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

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

    语法

    if (condition1)
    {
        当条件 1 为 true 时执行的代码
    }
    else if (condition2)
    {
        当条件 2 为 true 时执行的代码
    }
    else
    {
      当条件 1 和 条件 2 都不为 true 时执行的代码
    }
  • 相关阅读:
    Angular 组件通信的三种方式
    Vue 之keep-alive的使用,实现页面缓存
    Angular Service设计理念及使用
    Git提交规范
    angular的生命周期
    CPU 是如何认识和执行代码的
    Ubuntu 常用软件
    UltraSQL / sqlserver-kit (SQLServer DBA )高手
    便宜的网站模板下载
    Audio over Bluetooth: most detailed information about profiles, codecs, and devices
  • 原文地址:https://www.cnblogs.com/gengyufei/p/12594652.html
Copyright © 2011-2022 走看看