Last active
December 17, 2015 05:38
-
-
Save commuterjoy/5558988 to your computer and use it in GitHub Desktop.
Convert a string to it's correct datatype (number, boolean, or 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
function isNumeric(str){ | |
return !isNaN(str); | |
} | |
function isBoolean(str){ | |
return (str === "true" || str === "false"); | |
} | |
// 1. +val casts any number (int, float) from a string | |
// 2. String(val) === "true" converts a string to bool | |
var v = (isNumeric(val) ? +val : isBoolean(val) ? (String(val).toLowerCase() === "true") : val); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment