Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Last active December 17, 2015 05:38
Show Gist options
  • Save commuterjoy/5558988 to your computer and use it in GitHub Desktop.
Save commuterjoy/5558988 to your computer and use it in GitHub Desktop.
Convert a string to it's correct datatype (number, boolean, or string)
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