Heim  >  Artikel  >  Web-Frontend  >  js比较和逻辑运算符的介绍_基础知识

js比较和逻辑运算符的介绍_基础知识

WBOY
WBOYOriginal
2016-05-16 17:40:331622Durchsuche

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

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

给定 x=5,下面的表格解释了比较运算符:

运算符 描述 例子
== 等于 x==8 为 false
=== 全等(值和类型) x===5 为 true;x==="5" 为 false
!= 不等于 x!=8 为 true
> 大于 x>8 为 false
小于 x
>= 大于或等于 x>=8 为 false
小于或等于 x
如何使用
可以在条件语句中使用比较运算符对值进行比较,然后根据结果来采取行动:

if (age您将在本教程的下一节中学习更多有关条件语句的知识。

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

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

运算符 描述 例子
&& and (x 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"。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn