JavaScript判断相不相等的方法:1、使用“==”或“!=”运算符,比较两个操作数的值是否相等;2、使用“===”或“!==”运算符,比较两个操作数的值是否相等,同时检测它们的类型是否相同。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
JavaScript判断相不相等
在JavaScript中,可以利用==
、===
、!=
、!==
运算符来判断相等或者不等于
等值检测运算符 | 说明 |
---|---|
==(相等) | 比较两个操作数的值是否相等 |
!=(不想等) | 比较两个操作数的值是否不相等 |
===(全等) | 比较两个操作数的值是否相等,同时检测它们的类型是否相同 |
!==(不全等) | 比较两个操作数的值是否不相等,同时检测它们的类型是否不相同 |
在相等运算中,应注意以下几个问题:
如果操作数是布尔值,则先转换为数值,其中 false 转为 0,true 转换为 1。
如果一个操作数是字符串,另一个操作数是数字,则先尝试把字符串转换为数字。
如果一个操作数是字符串,另一个操作数是对象,则先尝试把对象转换为字符串。
如果一个操作数是数字,另一个操作数是对象,则先尝试把对象转换为数字。
如果两个操作数都是对象,则比较引用地址。如果引用地址相同,则相等;否则不等。
示例1
下面是特殊操作数的相等比较。
console.log("1" == 1); //返回true。字符串被转换为数字 console.log(true == 1); //返回true。true被转换为1 console.log(false == 0); //返回true。false被转换为0 console.log(null == 0); //返回false console.log(undefined == 0); //返回false console.log(undefined == null); //返回true console.log(NaN == "NaN"); //返回false console.log(NaN ==1); //返回false console.log(NaN == NaN); //返回false console.log(NaN != NaN); //返回true
NaN与任何值都不相等,包括它自己。null 和 undefined 值相等,但是它们是不同类型的数据。在相等比较中,null 和 undefined 不允许被转换为其他类型的值。
示例2
下面两个变量的值是相等的。
var a = "abc" + "d"; var b = "a" + "bcd"; console.log(a == b); //返回true
数值和布尔值的相等比较运算效率比较高,而字符串需要逐个字符进行比较,相等比较运算效率比较低。
在全等运算中,应注意以下几个问题:
如果两个操作数都是简单的值,则只要值相等,类型相同,就全等。
如果一个操作数是简单的值,另一个操作数是复合型对象,则不全等。
如果两个操作数都是复合型对象,则比较引用地址是否相同。
示例3
下面是特殊操作数的全等比较。
console.log(null === undefined); //返回false console.log(0 === "0"); //返回false console.log(0 === false); //返回false
示例4
下面是两个对象的比较,由于它们都引用了相同的地址,所以返回 true。
var a = {}; var b = a; console.log(a === b); //返回true
下面两个对象虽然结构相同,但是地址不同,所以不全等。
var a = {}; var b = {}; console.log(a === b); //返回false
示例5
对于复合型对象,主要比较引用的地址,不比较对象的值。
var a = new String("abcd); //定义字符串“abcd”对象 var b = new String("abcd); //定义字符串“abcd”对象 console.log(a === b); //返回false console.log(a == b); //返回false
在上面示例中,两个对象的值相等,但是引用地址不同,所以它们既不想等,也不全等。因此,对于复合型对象来说,相等==和全等===运算的结果是相同的。
示例6
对于简单的值,只要类型相同、值相等,它们就是全等,不考虑表达式运算的过程变化,也不用考虑变量的引用地址。
var a = "1" + 1; var b = "11"; console.log(a ===b); //返回true
示例7
表达式(a>b || a==b)与表达式(a>=b)并不完全相等。
var a = 1; var b = 2; console.log((a > b || a == b) == (a >= b)); //返回true,此时似乎相等
如果为变量 a 和 b 分别赋值 null 和 undefined,则返回值 false,说明这两个表达式并非完全等价。
var a = null; var b = undefined; console.log((a > b || a == b) == (a >= b)); //返回false,表达式的值并非相等
因为 null == undefined 等于 true,所以表达式(a > b || a == b)的返回值为 true,但是表达式 null >= undefined 的返回值为 false。
【相关推荐:javascript学习教程】
以上是JavaScript怎么判断相不相等的详细内容。更多信息请关注PHP中文网其他相关文章!

Include:1)AsteeplearningCurvedUetoItsVasteCosystem,2)SeochallengesWithClient-SiderEndering,3)潜在的PersperformanceissuesInsuesInlArgeApplications,4)ComplexStateStateManagementAsappsgrow和5)TheneedtokeEedtokeEedtokeEppwithitsrapideDrapidevoltolution.thereedtokeEppectortorservolution.thereedthersrapidevolution.ththesefactorsshesssheou

reactischallengingforbeginnersduetoitssteplearningcurveandparadigmshifttocoment oparchitecent.1)startwithofficialdocumentationforasolidFoundation.2)了解jsxandhowtoembedjavascriptwithinit.3)

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

javascriptfatigueinrectismanagbaiblewithstrategiesLike just just in-timelearninganning and CuratedInformationsources.1)学习whatyouneedwhenyouneedit

totlecteactComponents通过theusestatehook,使用jestandReaCtteTingLibraryToSigulation Interactions andverifyStatAtaTeChangesInTheUI.1)renderthecomponentAndComponentAndComponentAndCheckInitialState.2)模拟useclicklicksorformsormissionsions.3)

KeysinreactarecrucialforopTimizingPerformanceByingIneFefitedListupDates.1)useKeyStoIndentifyAndTrackListelements.2)避免使用ArrayIndicesasKeystopreventperformansissues.3)ChooSestableIdentifierslikeIdentifierSlikeItem.idtomaintainAinainCommaintOnconMaintOmentStateAteanDimpperperFermerfermperfermerformperfermerformfermerformfermerformfermerment.ChosestopReventPerformissues.3)

ReactKeySareUniqueIdentifiers usedwhenrenderingListstoimprovereConciliation效率。1)heelPreactrackChangesInListItems,2)使用StableanDuniqueIdentifiersLikeItifiersLikeItemidSisRecumended,3)避免使用ArrayIndicesaskeyindicesaskeystopreventopReventOpReventSissUseSuseSuseWithReRefers和4)

独特的keysarecrucialinreactforoptimizingRendering和MaintainingComponentStateTegrity.1)useanaturalAlaluniqueIdentifierFromyourDataiFabable.2)ifnonaturalalientedifierexistsistsists,generateauniqueKeyniqueKeyKeyLiquekeyperaliqeyAliqueLiqueAlighatiSaliqueLiberaryLlikikeuuId.3)deversearrayIndiceSaskeyseSecialIndiceSeasseAsialIndiceAseAsialIndiceAsiall


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器