Created
July 9, 2017 09:09
-
-
Save juliomatcom/1a6e25d7dbe20707e5dae771d14c4c10 to your computer and use it in GitHub Desktop.
Type conversion with Boolean, Number and String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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