JavaScript 比较 和 逻辑运算符

比较运算符

运算符    说明        例子        运算结果

==    等于    2 == 3    FALSE    

===    恒等于(值和类型都要做比较)    (2 === 2 TRUE)   (2 === "2"    FALSE )   

!=    不等于,也可写作<>    2 == 3    TRUE    

>    大于    2 > 3    FALSE    

<    小于    2 < 3    TRUE    

>=    大于等于    2 >= 3    FALSE    

<=    小于等于    2 <= 3    TRUE    

比较运算符也可用于字符串比较。

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">
        var a=5,b="5";
        document.write(a==b);
        document.write("<br />");
        document.write(a===b);
        document.write("<br />");
        document.write(a!=b);
        document.write("<br />");
        document.write(a!==b);
    </script>
</head>
<body>
</body>
</html>


逻辑运算符

运算符    说明     例子     运算结果

&&    逻辑与(and)    x = 2;   y = 6;   x && y > 5    FALSE    

||    逻辑或(or)    x = 2;   y = 6;    x || y > 5    TRUE    

!    逻辑非,取逻辑的反面    x = 2;   y = 6;   !(x > y)    TRUE    

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">
        var a=5,b="5";
        document.write(a&&b > 10);
        document.write("<br />");
        document.write(a||b <6);
        document.write("<br />");
        document.write(!(a>b));
      </script>
</head>
<body>
</body>
</html>


条件运算符

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

语法

variablename=(condition)?value1:value2 

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
年龄:<input id="age" value="18" />
<p>是否达到上学年龄?</p>
<button onclick="myFunction()">点击按钮</button>
<p id="demo"></p>
<script>
function myFunction()
{
var age,voteable;
age=document.getElementById("age").value;
voteable=(age<7)?"年龄太小,继续幼儿园":"年龄已达到,可以入学";
document.getElementById("demo").innerHTML=voteable;
}
</script>
</body>
</html>



继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var a=5,b="5"; document.write(a==b); document.write("<br />"); document.write(a===b); document.write("<br />"); document.write(a!=b); document.write("<br />"); document.write(a!==b); </script> </head> <body> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

JavaScript学习指南

高并发千万级数据库系统解决方案
  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载

我喜欢晴天

逻辑运算符不大理解啊

7年前    添加回复 0

Alway.以为

了解就好,知道有这些运算符

7年前    添加回复 0

大神,求带!

偶巴,好厉害。继续下一节吧。

7年前    添加回复 0

素颜

还有这么多的运算符啊

7年前    添加回复 0

js比较运算符==与===的区别?

[最新 数据分析师 的回答] js比较运算符==与===的区别?-PHP中文网问答-js比较运算符==与===的区别?-PHP中文网问答围观一下哦,学习一下。

时间:7年前

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~