How to convert a numeric value to Boolean?

Asked

Viewed 664 times

1

I get a value boolean of the database, in which I intend to define a checkbox dynamically. The value received is 0 or 1. I tried to do it this way below:

var status = 1; //valor recebido
$("#status").prop('checked', status);  

But in agreement with the documentation of the prop method, should be received as a parameter boolean or a function, not a numeral.

How do I convert a numeric value into boolean?

3 answers

2


If you’re sure the entrance is int can directly apply 0 or 1 to the .checked = status; (even though .prop('checked', status)) (as @Sergio explained):

var status = 1; //valor recebido

$("#status").prop('checked', status);

But for other cases where there is no automatic conversion you use so with !!, as long as you’re sure to be a int:

var status = 1; //valor recebido

$("#status").prop('checked', !!status);

Or so using "ternary operator":

var status = 1; //valor recebido

$("#status").prop('checked', status ? true : false);

Now if you’re not sure you’re getting one you’re getting string you can check like this:

var status = '1'; //valor recebido

$("#status").prop('checked', status == '1');

If you just want to "convert" integers to booleans:

var status = 1;

console.log('Com ==', status == '1'); //Este talvez seja o mais garantido
console.log('Com !!', !!status);
console.log('Com ternário', status ? true : false);

  • There’s no need !! it is converted into boolean even with native Javascript.

  • @Sergio Para .prop() or .checked = 1 you are correct, the question that I pointed this to any situation where there is no "automatic" conversion, to similar doubts (is that I focused on the title).

  • It would not be better to force the int using !!parseInt(status), because if it is string will result in true in both cases. : S

  • @Inkeliz yes, in the case of 1 and 2, but as AP had placed a var status = 1; I assumed the entrance would always be int and another I followed the title, but I will add a better explanation.

2

test the guy of that variable first. If string will always give true. Checks with:

console.log(typeof status, status);

To be sure use Number(status), or status == '1' ? true : false.

$("#status").prop('checked', status == '1' ? true : false);  

Examples:

var status = '1';
$('#statusA').prop('checked', Number(status));
$('#statusB').prop('checked', status == '1' ? true : false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="statusA" type="checkbox">
<input id="statusB" type="checkbox">
<input id="status_controle" type="checkbox">

0

I would most likely do something like this:

var status = 1; // value received

$("#status"). prop('checked', Number(status) ? true : false);

Using ternary same, and converting the value always in a Number, in case of come a value above 0 would be true and otherwise would be false.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.