// To convert a boolean to a string we use the .toString() method
let isValid = true;
console.log(isValid.toString()); // outputs "true"
console.log(isValid); // outputs true
JSON.parse('true')
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;
}
}
// Everyone does one extra check. Here is a better answer
let toBool = string => string === 'true'; // ? true : false;
// Not everyone gets ES6 so here for the beginners
function toBool(string){
return string === 'true';
}
// In React Or Node Js Or Any JavaScript Framework
const AnyName = JSON.parse("true"); //YourSreing
//Now You data converted Boolean Value
// This is how it should be if you store the database
AnyName : true,
// Do
var isTrueSet = (myValue == 'true');
// Or
var isTrueSet = (myValue === 'true');
booleanToString = b => { return b.toString(); }
// Way cleaner Version! easy readability!!
bool.toString()
ES6+
const string = "false"
const string2 = "true"
const test = (val) => (val === "true" || val === "True")
console.log(test(string))
console.log(test(string2))