JavaScript : Convert Boolean to Number / Integer

/ / 0 Comments

Convert the Boolean to Int: Here in this article, we learn how to convert Boolean to an integer value in JavaScript. While programming when we need to work with  Yes/No, ON/OFF, or true/false values, we use Boolean datatype. As we know Boolean value is either true or false, converting to an integer gives us 1 or 0  respectively. Here we going to learn 5 different ways to return the Boolean results as an integer value in JavaScript.i.e ( In js boolean to int conversion) 

5 ways to convert Boolean to number in JS

  1. Using Conditional (ternary) operator to convert Boolean to number
  2. Using JavaScript Number() function to return Boolean as integer
  3. Unary + operation
  4. Using bitwise AND operator to convert Boolean to 1 or 0.
  5. Using bitwise OR operator.

#1 Use Conditional (ternary) operator

Here first we declare a boolean variable IsLoggedIn and set its value as true. Now we add a button tag with an onclick event. Onclick event we call our javascript function which converts boolean to numeric using ternary operator.

HTML Markup:

<script>
 var IsLoggedIn=true;
</script>
<label>Using ternary operator <label>
<input type="button" value="Convert Boolean to Number" onclick="fnConvertBooleanToNumber()" />

JavaScript code:

 function fnConvertBooleanToNumber() { 
	  var convertedBoolValue = IsLoggedIn ? 1 : 0; 
	  alert(convertedBoolValue); 
 }

Output:

Here it popup an alert message, which returns 1 as an integer value.


#2 Using JavaScript Number() function

Same here we use a button tag and using the javascript built-in Number() method we transform boolean to an integer. JavaScript Code Convert boolean to number is as follow:

function fnConvertBooleanToNumber(){ 
     var convertedBoolValue = Number(IsLoggedIn); 
     alert(convertedBoolValue); 
 } 

#3 Unary + operation

Here we use the Unary plus operation, to convert it into an integer value. Basically, we add a plus sign before our boolean variable, and with this single line code, we achieve bool to int js conversion.

Our JS function is as below:

function fnConvertBooleanToNumber(){ 
         var convertedBoolValue = + IsLoggedIn ; 
         alert(convertedBoolValue );
  }

#4 Using bitwise AND operator

Here we use the bitwise AND operator, and it gives us numeric value from the boolean result. As you see with this simple javascript code we able to convert boolean to number.

JS code is as below:

function fnConvertBooleanToNumber(){ 
     var convertedBoolValue =  IsLoggedIn & 1; // bitwise AND operator
      alert(convertedBoolValue); 
 } 

#5 Using bitwise OR operator

Last but not least here we use the bitwise OR operator, so we can convert bool value to number in JavaScript.

Our JS function is as written below:

function fnConvertBooleanToNumber(){ 
     var convertedBoolValue =  IsLoggedIn | 0; // bitwise OR operator  
     alert(convertedBoolValue); 
 }


Conclusion: Here in this article we learn 5 ways to convert the boolean value into an integer value (number). If one asks me I simply prefer conditional ternary operator, what you prefer write below in the comment section.

Other Reference :

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments