Skip to content

Instantly share code, notes, and snippets.

@0bie
Last active November 8, 2017 19:21
Show Gist options
  • Save 0bie/e3f58feaba2ab61f1535f9e732d46af1 to your computer and use it in GitHub Desktop.
Save 0bie/e3f58feaba2ab61f1535f9e732d46af1 to your computer and use it in GitHub Desktop.
Convert values to boolean in JS
// In JS `!!` can be used to convert any value to a boolean
// Similar to using the Boolean() function
(function() {
var foo = 'foo';
console.log(typeof foo);
var bool = !!foo;
console.log(typeof bool);
console.log(bool);
})()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
// http://brianflove.com/2014/09/02/whats-the-double-exclamation-mark-for-in-javascript/
// https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript#answer-15035152
// https://stackoverflow.com/questions/29312123/how-does-the-double-exclamation-work-in-javascript#answer-29312197
// https://medium.com/@pddivine/javascript-bang-bang-i-shot-you-down-use-of-double-bangs-in-javascript-7c9d94446054
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment