# Convert Boolean

## Answered: Convert Boolean

Sencha doesn't seem to use the same logic for converting to boolean as JS which leads to some confusion.

Ext.data.Types does the conversion with
Code:
```if (this.useNull && (v === undefined || v === null || v === '')) {
return null;}
return v === true || v === 'true' || v == 1;```
which means things like -1 and most other truthy combinations don't return true.

Why isn't it simply
Code:
```if (v)return true;elsereturn false;

```
which would then match JS?

2. I did forget the usenull, so would need to copy that line into my fix.

As for -1 in JS not being true, it isn't == true but it is truthy.

Also, the code doesn't just check if it exists otherwise v=0 would return true.
Code:
```  BOOL: {
convert: function (v) {
if (this.useNull && (v === undefined || v === null || v === '')) {
return null;

if (v)
return true;
else
return false;
},
sortType: st.none,
type: 'bool'
}```
Other option which is less intuitive but same result

Code:
```  BOOL: {
convert: function (v) {
if (this.useNull && (v === undefined || v === null || v === '')) {
return null;

return !!v;
},
sortType: st.none,
type: 'bool'
}```

3. your code is just checking if v exists not if it is true or false. And besides, I don't think -1 in Javascript is true.

For example, if you set v to -1 and run this code you will get false and this is basically what the code in Ext.data.Types is doing except it will return null if the useNull property is set to true.

Code:
```if (v == true) {
return true;
} else {
return false;
}```

4. I did forget the usenull, so would need to copy that line into my fix.

As for -1 in JS not being true, it isn't == true but it is truthy.

Also, the code doesn't just check if it exists otherwise v=0 would return true.
Code:
```  BOOL: {
convert: function (v) {
if (this.useNull && (v === undefined || v === null || v === '')) {
return null;

if (v)
return true;
else
return false;
},
sortType: st.none,
type: 'bool'
}```
Other option which is less intuitive but same result

Code:
```  BOOL: {
convert: function (v) {
if (this.useNull && (v === undefined || v === null || v === '')) {
return null;

return !!v;
},
sortType: st.none,
type: 'bool'
}```

5. Ah yes, you're right. -1 is truthy. Perhaps you can post this in the Bugs forum if you want the core devs to consider changing the code.