Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juliomatcom/1a6e25d7dbe20707e5dae771d14c4c10 to your computer and use it in GitHub Desktop.
Save juliomatcom/1a6e25d7dbe20707e5dae771d14c4c10 to your computer and use it in GitHub Desktop.
Type conversion with Boolean, Number and String
Boolean(""); // false
Boolean(0); // false
Boolean(1); // true
Number("10"); // 10
Number("foo"); // NaN
String(10); // "10"
var stuff = ["", 1, undefined, { foo: "bar" }];
// Get only the truthy values inside stuff
var onlyValid = stuff.filter(Boolean); // [ 1, { foo: "bar" } ]
// Get only the numbers values inside stuff (Note: 0 is a falsy value so this won't work if 0 is in stuff)
var onlyNumbers = stuff.filter(Number); // [ 1 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment