Skip to content

Instantly share code, notes, and snippets.

@JosephClay
Last active August 29, 2015 14:13
Show Gist options
  • Save JosephClay/81f2a4ee080add6d9913 to your computer and use it in GitHub Desktop.
Save JosephClay/81f2a4ee080add6d9913 to your computer and use it in GitHub Desktop.
JS: typecast
var typecast = function(val) {
// Pulled from AMD
var r;
if (val === null || val === 'null') {
r = null;
} else if (val === 'true') {
r = true;
} else if (val === 'false') {
r = false;
} else if (val === undefined || val === 'undefined') {
r = undefined;
} else if (val === '' || isNaN(val)) {
// isNaN('') returns false
r = val;
} else {
// parseFloat(null || '') returns NaN
r = parseFloat(val);
}
return r;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment