Js! The usage is more flexible, it is often used in addition to doing logical operations! Do type judgment, can use! To obtain a Boolean value
1,! Variables can be converted to Boolean type, null, undefined, and empty string are true, the rest are false.
!' '=true
!0=true
!null=true
!undefined=true
Copy the code
A string value that converts null values (“”) to false and the rest to true.
Numeric type that converts 0 to false and the rest to true.
Null and undefined convert to false.
Therefore, null, undefined, 0, and “” are all converted to flase.
alert(!!undefined)//false
alert(!!null)//false
alert(!!0)//false
alert(!!"")//fase
Copy the code
A!!! Is to convert an object to a Boolean and invert it. Two! Is invert a Boolean value, which is equivalent to converting a non-Boolean value to a Boolean value.