码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript--比较

时间:2015-06-01 18:56:56      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:



比较和逻辑运算符用于测试 true 或 false。
比较运算符

比较运算符在逻辑语句中使用,以测定变量或值是否相等。

给定 x=5,下面的表格解释了比较运算符:
运算符     描述     例子
==     等于     x==8 为 false
===     全等(值和类型)     x===5 为 true;x==="5" 为 false
!=     不等于     x!=8 为 true
>     大于     x>8 为 false
<     小于     x<8 为 true
>=     大于或等于     x>=8 为 false
<=     小于或等于     x<=8 为 true
如何使用

可以在条件语句中使用比较运算符对值进行比较,然后根据结果来采取行动:

if (age<18) document.write("Too young");

您将在本教程的下一节中学习更多有关条件语句的知识。
逻辑运算符

逻辑运算符用于测定变量或值之间的逻辑。

给定 x=6 以及 y=3,下表解释了逻辑运算符:
运算符     描述     例子
&&     and     (x < 10 && y > 1) 为 true
||     or     (x==5 || y==5) 为 false
!     not     !(x==y) 为 true
条件运算符

JavaScript 还包含了基于某些条件对变量进行赋值的条件运算符。
语法

variablename=(condition)?value1:value2

例子

greeting=(visitor=="PRES")?"Dear President ":"Dear ";

如果变量 visitor 中的值是 "PRES",则向变量 greeting 赋值 "Dear President ",否则赋值 "Dear"。




<!DOCTYPE html>
<html>
<body>
<meta http-equiv="Content-Type" content="text/html charset=utf-8"/>


<script>
    function Print()
    {

        var age = 15;
        //if(age < 18)
        //    document.write("Too young");

        var visitor = "PRES";
        var  greeting=(visitor=="PRES")?"Dear President ":"Dear";
        document.write(greeting);

    }
</script>

<td><input type="text" name="txtRow1" id="txt1" size="40" /></td>

<button onclick="Print()">点击这里</button>

</body>
</html>



JavaScript--比较

标签:

原文地址:http://blog.csdn.net/u012701023/article/details/46313015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!