0
Q:

javascript convert to boolean

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);
    }
}
3
var input = 1;
var bool = Boolean(input); // true
0
// Do
var isTrueSet = (myValue == 'true');
// Or
var isTrueSet = (myValue === 'true');
2
var isHidden='true';
var isHiddenBool = (isHidden == 'true');
2
const stringBools = [
  'true',
  'false',
  'asdf'
];
const bools = [];

for (const i = 0; i < stringBools.length; i++) {
  const stringBool = stringBools[i];
  if (stringBool == true) {
    bools.push(true);
  } else if (stringBool == false) {
    bools.push(false);
  } else {
    bools.push(null /* Change if you like */);
  }
}
0

New to Communities?

Join the community