Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created April 30, 2018 17:43
Show Gist options
  • Save gladchinda/ab3f7dab633ac7b79657eb5d23e247ff to your computer and use it in GitHub Desktop.
Save gladchinda/ab3f7dab633ac7b79657eb5d23e247ff to your computer and use it in GitHub Desktop.
// Function that accepts a boolean as first argument
// and returns the boolean otherwise `false`
const booleanOrFalse = value => (typeof value == 'boolean') && value;
// Function that accepts a boolean as first argument
// and returns the boolean otherwise `true`
const booleanOrTrue = value => (typeof value != 'boolean') || value;
console.log(booleanOrFalse(true)); // true
console.log(booleanOrFalse(false)); // false
console.log(booleanOrFalse(1)); // false
console.log(booleanOrFalse(0)); // false
console.log(booleanOrFalse('String')); // false
console.log(booleanOrFalse({})); // false
console.log(booleanOrFalse([])); // false
console.log(booleanOrTrue(true)); // true
console.log(booleanOrTrue(false)); // false
console.log(booleanOrTrue(1)); // true
console.log(booleanOrTrue(0)); // true
console.log(booleanOrTrue('String')); // true
console.log(booleanOrTrue({})); // true
console.log(booleanOrTrue([])); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment