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}
1stringToBoolean: function(string){
2 switch(string.toLowerCase().trim()){
3 case "true": case "yes": case "1": return true;
4 case "false": case "no": case "0": case null: return false;
5 default: return Boolean(string);
6 }
7}
1const stringBools = [
2 'true',
3 'false',
4 'asdf'
5];
6const bools = [];
7
8for (const i = 0; i < stringBools.length; i++) {
9 const stringBool = stringBools[i];
10 if (stringBool == true) {
11 bools.push(true);
12 } else if (stringBool == false) {
13 bools.push(false);
14 } else {
15 bools.push(null /* Change if you like */);
16 }
17}