js运算符 ~~ 是什么意思?

开发过程中,我们经常会用运算符: + - == != && || 等等,但今天翻看项目源码的时候发现了一个奇特的写法 : ~~

 

因为比较少见,所以在此记录一下:

其实就是将后面的变量转化为数字类型,但只能判断可等价转换的类型。例如:

console.log(~~true)            // 结果为1

console.log(~~false)          // 结果为0

console.log(~~null)            // 结果为0

console.log(~~!null)           // 结果为1

console.log(~~0)               // 结果为0

console.log(~~1)               // 结果为1

console.log(~~123456)     // 结果为123456

// 注意 只能转为数字类型 以下写法输出是0

console.log(~~"")                              // 结果为0

console.log(~~"我不是空字符串")     // 结果为0

console.log(~~[])                               //结果为0

console.log(~~[1,2,3])                       //结果为0

console.log(~~{})                               //结果为0

console.log(~~{a:1,b:2})                    //结果为0

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值