1// To convert a boolean to a string we use the .toString() method
2let isValid = true;
3
4console.log(isValid.toString()); // outputs "true"
5console.log(isValid); // outputs true
6
1var myBool = Boolean("false"); // == true
2
3var myBool = !!"false"; // == true
1let toBool = string => string === 'true' ? true : false;
2// Not everyone gets ES6 so here for the beginners
3function toBool(string){
4 if(string === 'true'){
5 return true;
6 } else {
7 return false;
8 }
9}
1// Do
2var isTrueSet = (myValue == 'true');
3// Or
4var isTrueSet = (myValue === 'true');
1// Everyone does one extra check. Here is a better answer
2
3let toBool = string => string === 'true'; // ? true : false;
4// Not everyone gets ES6 so here for the beginners
5function toBool(string){
6 return string === 'true';
7}
1booleanToString = b => { return b.toString(); }
2// Way cleaner Version! easy readability!!