JavaScript中比较运算符隐式类型转换的介绍(附示例)


本文摘自PHP中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于JavaScript中比较运算符隐式类型转换的介绍(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

相信大家在代码中经常看见 '==' 和 '===',但大家真的弄懂了比较运算符和其中的隐式转换嘛? 今天就重新认识下比较运算符。

全等运算符 ===

说明: 严格匹配,不会类型转换,必须要数据类型和值完全一致

1

2

3

4

5

6

7

8

9

10

11

12

先判断类型,如果类型不是同一类型的话直接为false;

 

1 对于基本数据类型(值类型): Number,String,Boolean,Null和Undefined:两边的值要一致,才相等

      console.log(null === null)   // true

      console.log(undefined === undefined)  // true

   注意: NaN: 不会等于任何数,包括它自己

   console.log(NaN === NaN)  // false

 

2 对于复杂数据类型(引用类型): Object,Array,Function等:两边的引用地址如果一致的话,是相等的

     arr1 = [1,2,3];

     arr2 = arr1;

     console.log(arr1 === arr2)   // true

相等运算符 ==

非严格匹配: 会类型转换,但是有前提条件一共有五种情况
(接下来的代码以 x == y 为示例)

x和y都是null或undefined:
规则: 没有隐式类型转换,无条件返回true

1

2

3

console.log ( null == undefined );//true

console.log ( null == null );//true

console.log ( undefined == undefined );//true

x或y是NaN : NaN与任何数字都不等
规则:没有隐式类型转换,无条件返回false

1

console.log ( NaN == NaN );//false

x和y都是string,boolean,number
规则:有隐式类型转换,会将不是number类型的数据转成number

1

2

3

4

5

console.log ( 1 == true );//true    (1) 1 == Number(true)

console.log ( 1 == "true" );//false   (1) 1 == Number('true')

console.log ( 1 == ! "true" );//false  (1) 1 == !Boolean('true')  (2) 1 == !true  (3) 1 == false  (4)1 == Number(false)

console.log ( 0 == ! "true" );//true

console.log(true == 'true') // false

x或y是复杂数据类型 : 会先获取复杂数据类型的原始值之后再左比较
复杂数据类型的原始值: 先调用valueOf方法,然后调用toString方法
valueOf:一般默认返回自身
数组的toString:默认会调用join方法拼接每个元素并且返回拼接后的字符串

1

2

3

4

5

6

7

8

console.log ( [].toString () );//空字符串

console.log ( {}.toString () );//[object Object]

注意:  空数组的toString()方法会得到空字符串,

      而空对象的toString()方法会得到字符串[object Object] (注意第一个小写o,第二个大写O哟)

 

console.log ( [ 1, 2, 3 ].valueOf().toString());//‘1,2,3’

console.log ( [ 1, 2, 3 ] == "1,2,3" );//true  (1)[1,2,3].toString() == '1,2,3'  (2)'1,2,3' == '1,2,3'

console.log({} == '[object Object]');//true

x和y都是复杂数据类型 :
规则只比较地址,如果地址一致则返回true,否则返回false

1

2

3

4

5

6

7

8

9

var arr1 = [10,20,30];

var arr2 = [10,20,30];

var arr3 = arr1;//将arr1的地址拷贝给arr3

        

console.log ( arr1 == arr2 );//虽然arr1与arr2中的数据是一样,但是它们两个不同的地址

console.log ( arr3 == arr1 );//true  两者地址是一样

        

console.log ( [] == [] );//false

console.log ( {} == {} );//false

经典面试题

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

注意:八种情况转boolean得到false: 0 -0 NaN undefined null '' false document.all()

 

console.log([] == 0); //true

  // 分析:(1) [].valueOf().toString() == 0  (2) Number('') == 0  (3) false == 0  (4) 0 == 0

console.log(![] == 0); //true

  // 分析: 逻辑非优先级高于关系运算符 ![] = false (空数组转布尔值得到true)

         

console.log([] == []); //false

// [] 与右边逻辑非表达式结果比较

//(1) [] == !Boolean([])   (2) [] == !true  (3)[] == false  (4) [].toString() == false  (5)'' == false   (6)Number('0') == Number(false)

console.log([] == ![]); //true

 

onsole.log({} == {}); //false

// {} 与右边逻辑非表达式结果比较

//(1){} == !{} (2){} == !true  (3){} == false  (4){}.toString() == false  (5)'[object Object]' == false  (6)Number('[object Object]') == false

console.log({} == !{}); //false

变态面试题

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

var  a = ???

  if(a == 1 && a == 2 && a == 3 ){

      console.log(1)

  }

 

//如何完善a,使其正确打印1

 

 

//答案

var a = {

  i : 0,    //声明一个属性i

    valueOf:function ( ) {

     return ++a.i;    //每调用一次,让对象a的i属性自增一次并且返回

    }

 }

 if (a == 1 && a == 2 && a == 3){  //每一次运算时都会调用一次a的valueOf()方法

  console.log ( "1" );

 }

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的JavaScript视频教程栏目!

以上就是JavaScript中比较运算符隐式类型转换的介绍(附示例)的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript怎么判断是否数字

javascript数组中常用的操作介绍(代码示例)

javascript中的arguments对象的用法介绍

一起了解script标签中的async和defer属性

HTML5中在用户可以开始播放视频/音频时触发的事件oncanplay

javascript变量的命名规则是什么

HTML5 video视频字幕的使用和制作方法

javascript vs dart 两者之间的区别与作用

javascript中map方法怎么用

html页面自动清理js、css文件的缓存(自动添加版本号)_html/xhtml_网页制作

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...