JavaScript 比较和逻辑运算符

来源:互联网 发布:千里眼定位软件 编辑:程序博客网 时间:2024/05/19 09:13

比较运算符

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

运算符描述例子==等于x==8 为 false===全等(值和类型)x===5 为 true;x==="5" 为 false!=不等于x!=8 为 true>大于x>8 为 false<小于x<8 为 true>=大于或等于x>=8 为 false<=小于或等于x<=8 为 true

例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><body><script>var age=22;if (age<18){ document.write("Too young");}else{document.write("too old");}</sipt></body></html>

逻辑运算符

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

给定 x=6 以及 y=3,下表解释了逻辑运算符:

运算符描述例子&&and(x < 10 && y > 1) 为 true||or(x==5 || y==5) 为 false!not!(x==y) 为 true

例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><body><script>var x = 2;if(x<10 && x>1){document.write("true");}else{document.write("false");}</script></body></html>
原创粉丝点击