Skip to content

Instantly share code, notes, and snippets.

@kostasx
Created September 18, 2014 02:38
Show Gist options
  • Save kostasx/361da6eca0480d57c97e to your computer and use it in GitHub Desktop.
Save kostasx/361da6eca0480d57c97e to your computer and use it in GitHub Desktop.
Javascript boolean caster
/*
* Convert values: 1, 0, true, false, "1", "0", "true", "false", "TRUE", "FALSE", "True", "False"
* "ON", "OFF", "On", "Off", negative numbers, positive numbers, to booleans with a single JS line.
* Easily extensible with new truthy values.
*/
function checkBool(val){
return ({1:1,true:1,on:1,yes:1}[(((typeof val !=="number")?val:(val>0))+"").toLowerCase()])?true:false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment