01.条件运算符.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script>
  9. /*
  10. 条件运算符也叫三元运算符
  11. 语法:
  12. 条件表达式?语句1:语句2;
  13. -执行的流程:
  14. 条件运算符在执行时,首先对条件表达式进行求值,
  15. 如果该值为true,则执行语句1,并返回执行结果
  16. 如果该值为false,则执行语句2,并返回结果
  17. 如果条件表达式的求值结果是一个非布尔值,
  18. 会将其转换为布尔值,然后再运算
  19. */
  20. //false?alert("语句1"):alert("语句2");
  21. var a = 10;
  22. var b = 20;
  23. var c = 40;
  24. //a>b?alert("a大"):alert("b大");
  25. //获取a和b中的最大值
  26. // var max = a > b ? a : b;
  27. // console.log('max='+max);
  28. //获取a b c中的大值
  29. // max = max > c ? max : c;
  30. // console.log('max='+max);
  31. //这种写法不推荐使用,不方便阅读
  32. var max = a > b ? (a > c ? a : c ): (b > c ? b : c);
  33. console.log('max='+max);
  34. // "hello"?alert("语句1"):alert("语句2");
  35. </script>
  36. </head>
  37. <body>
  38. </body>
  39. </html>