How to usedouble NOT(!!) operator in Javascript
This operator converts numbers with the value of zero to false and all other numbers to true.
Example: In this example, we will use the double NOT operator to convert the number into a boolean value.
HTML
<!DOCTYPE html> < html > < body > < center > < h1 style = "color:green" > w3wiki </ h1 > < h4 > Click the button to change the number value into boolean. </ h4 > < button onclick = "myFunction()" >Change</ button > < p >The number value of the variable is :</ p > < p id = "result" ></ p > < script > // Initializing boolvalue as true let numvalue = (!!1) // JavaScript program to illustrate boolean // conversion using ternary operator function myFunction() { document.getElementById("result") .innerHTML = (numvalue); } </ script > </ center > </ body > </ html > |
Output:
How to convert Number to Boolean in JavaScript ?
We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e. “false” or “true”.
Below are the approaches:
Table of Content
- Boolean() method
- Using double NOT(!!) operator