Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Created November 9, 2012 14:09
Show Gist options
  • Save jasonrhodes/4045860 to your computer and use it in GitHub Desktop.
Save jasonrhodes/4045860 to your computer and use it in GitHub Desktop.
function stringToNumber(value) {
/**
* Interesting facts about JavaScript:
*
* 1. NaN != NaN
* 2. If you pass a string to parseFloat(), it returns NaN
*
* These facts can be exploited to convert string numbers into numbers.
*/
var floatValue = parseFloat(value);
return floatValue != floatValue ? value : floatValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment