Detailed Explanation of Double Symbol Operation in JavaScript

  • 2021-08-03 08:54:54
  • OfStack

1. Double Wave


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

Have a similar effect Math.floor .

Similar means that when dealing with positive numbers, if dealing with negative numbers, they are different:


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

Note:


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.
~~
 Is to 0 Calculate and value to 0 Close 

2. Double exclamation points


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

It acts like Boolean and converts the value to boolean value.

Summarize


Related articles: