本文摘自PHP中文网,作者anonymity,侵删。
JavaScript中的字符串比较大于(>), 小于(<)运算符
javascript字符串在进行大于(小于)比较时,会根据第一个不同的字符的ascii值码进行比较,当数字(number)与字符串(string)进行比较大小时,会强制的将数字(number)转换成字符串(string)然后再进行比较。
代码:
1 2 3 4 5 6 7 | ( function (){
console.log( '13' > '3' );
console.log(5> '6' );
console.log( 'd' > 'ABDC' )
console.log(19> 'ssf' )
console.log( 'A' > 'abcdef' )
})()
|
相等(==),严格相等(===)运算符
在进行相等(==)运算比较时,如果一边是字符,一边是数字,会先将字符串转换成数字再进行比较;严格相等(===)则不会进行类型转换,会比较类型是否相等。注NaN与任何值比较时都是false
1 2 3 4 5 6 7 8 | ( function (){
console.log( '6' ==6)
console.log( '6' ===6)
console.log(6===6)
console.log( 'abc' ==2)
console.log( 'abc' == 'abc' )
console.log( 'abc' === 'abc' )
})()
|
三. 一些特殊值的相等与严格相等比较
1 2 3 4 5 6 7 8 9 10 | ( function (){
console.log( null ==undefined)
console.log( null ===undefined)
console.log( null === null )
console.log(undefined===undefined)
console.log(NaN==undefined)
console.log(NaN== null )
console.log(NaN==NaN)
console.log(NaN===NaN)
})()
|
以上就是JS怎么比较两个字符串的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
javascript去除拼接字符串末尾符号的方法
分享一些javascript中鲜为人知的隐藏语法或技巧
vue.js的ul-li标签仿select标签
javascript怎么输出绝对值
js回调函数是什么
javascript对象怎么转换成字符串
javascript url怎么隐藏
javascript如何删除html元素
javascript中解析parseint()的怪异行为
javascript怎样修改div内容
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » JS怎么比较两个字符串