当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript中双符号的运算详解

JavaScript中双符号的运算详解

2019年03月22日  | 移动技术网IT编程  | 我要评论

一、双波浪号

var i = 5.1;
var j = 5.5;
console.log(~~i); // 5
console.log(~~j); // 5

作用类似math.floor

类似的意思是在处理正数的时候,如果处理负数就它俩就不同了:

~~-5.1 // 5
math.floor(-5.1) // -6
~~-5.5 // 5
math.floor(-5.5) // -6

注:

math.ceil(x)
returns the smallest integer greater than or equal to a number.
math.floor(x)
returns the largest integer less than or equal to a number.
~~
是向0计算,取值向0靠拢

二、双感叹号

var a = 1;
var b = null;
var c = '';
var d = 'code';
console.log(!!a); // true
console.log(!!b); // false
console.log(!!c); // false
console.log(!!d); // true

作用类似boolean,把值转换为boolean值。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网