converter string em js booleanos
var myBool = Boolean("false"); // == true
var myBool = !!"false"; // == true
Breakable Barracuda
var myBool = Boolean("false"); // == true
var myBool = !!"false"; // == true
let toBool = string => string === 'true' ? true : false;
// Not everyone gets ES6 so here for the beginners
function toBool(string){
if(string === 'true'){
return true;
} else {
return false;
}
}
stringToBoolean: function(string){
switch(string.toLowerCase().trim()){
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false;
default: return Boolean(string);
}
}
// Do
var isTrueSet = (myValue == 'true');
// Or
var isTrueSet = (myValue === 'true');
var isTrueSet = (myValue === 'true');
// Function definition:
function stringToBool(string) { // In the original the function is called stringZuBool(string) because it's German.
if (string === 'true') {
return true;
} else {
return false;
}
}
// Usage:
stringToBool('Hallo!') // returns false
stringToBool("Hallo!") // returns false
stringToBool('true') // returns true
stringToBool("true") // returns true
stringToBool('false') // returns false
stringToBool("false") // returns false