Home > Article > Web Front-end > How to convert data to boolean value in javascript
Javascript method to convert data into Boolean values: 1. Use double logical negation, the syntax "!! The value that needs to be converted"; 2. Use the Boolean() function, the syntax "Boolean (the value that needs to be converted)" ".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There are 2 common methods for converting values into Boolean values, as detailed below.
1. Use double logical NOT
A logical NOT operator! You can convert the value into a Boolean value and negate it. Two logical NOT operators can convert The value is converted to the correct boolean value.
console.log(!!0); //返回false console.log(!!1); //返回true console.log(!!""); //返回false console.log(!!NaN); //返回false console.log(!!null); //返回false console.log(!!undefined); //返回false console.log(!![]); //返回true console.log(!!{}); //返回true console.log(!!function(){}); //返回true
2. Use the Boolean() function
Use the Boolean() function to force the value to be converted to a Boolean value.
console.log(Boolean(0)); //返回false console.log(Boolean(1)); //返回true
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to convert data to boolean value in javascript. For more information, please follow other related articles on the PHP Chinese website!