JavaScript : Convert Boolean to Number / Integer

/ Updated : / 0 Comments

Convert Boolean result to number: 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 integer gives us 1 or 0  respectively. Here we going to learn 5 different ways to return Boolean result as an integer value in JavaScript. 

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 button tag and using javascript number built-in method we transform boolean to integer. JavaScript Code Convert boolean to number is as follow:

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

#3 Unary + operation

Here we use Unary plus operation, to convert into an integer value. Basically, we add a plus sign before our boolean variable.  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 boolean result. 

JS code is as below:

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

#5 Using bitwise OR operator

Last but not the 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 do a favor, then 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