zoukankan      html  css  js  c++  java
  • 条件运算符

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6         <script type="text/javascript">
     7             /*
     8              * 条件运算符也叫三元运算符
     9              *     语法:
    10              *         条件表达式?语句1:语句2;
    11              *     - 执行的流程:
    12              *         条件运算符在执行时,首先对条件表达式进行求值,
    13              *             如果该值为true,则执行语句1,并返回执行结果
    14              *             如果该值为false,则执行语句2,并返回执行结果
    15              *         如果条件的表达式的求值结果是一个非布尔值,
    16              *             会将其转换为布尔值然后在运算
    17              */
    18             
    19             //false?alert("语句1"):alert("语句2");
    20             
    21             var a = 300;
    22             var b = 143;
    23             var c = 50;
    24             
    25             //a > b ? alert("a大"):alert("b大");
    26             
    27             //获取a和b中的最大值
    28             //var max = a > b ? a : b;
    29             //获取a b c 中的大值
    30             //max = max > c ? max : c;
    31             
    32             //这种写法不推荐使用,不方便阅读
    33             var max = a > b ? (a > c ? a :c) : (b > c ? b : c);
    34             
    35             //console.log("max = "+max);
    36             
    37             //"hello"?alert("语句1"):alert("语句2");
    38             
    39             
    40         </script>
    41     </head>
    42     <body>
    43     </body>
    44 </html>
  • 相关阅读:
    670. Maximum Swap
    653. Two Sum IV
    639. Decode Ways II
    636. Exclusive Time of Functions
    621. Task Scheduler
    572. Subtree of Another Tree
    554. Brick Wall
    543. Diameter of Binary Tree
    535. Encode and Decode TinyURL
    博客园自定义背景图片
  • 原文地址:https://www.cnblogs.com/lvjianqun/p/10307254.html
Copyright © 2011-2022 走看看