Last active
August 29, 2015 14:24
-
-
Save tahoeRobbo/5488d721f003896dd923 to your computer and use it in GitHub Desktop.
Ways of converting a stringified number back to a good ole number number
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
//Quick gist to prove the three ways (that I know of) to turn stringified numbers back into numbers in JavaScript. | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//This can be done using parseInt(stringifiedNum), Number(stringifiedNum), and +stringifiedNum. | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//See lines 10, 18, and 26 for usage | |
var add10 = function(numArray) { | |
var added10 = []; | |
for(var i = 0; i < numArray.length; i++) { | |
added10.push(+numArray[i] + 10); | |
} | |
return added10; | |
}; | |
var add10b = function(numArray) { | |
var added10 = []; | |
for(var i = 0; i < numArray.length; i++) { | |
added10.push(Number(numArray[i]) + 10); | |
} | |
return added10; | |
}; | |
var add10c = function(numArray) { | |
var added10 = []; | |
for(var i = 0; i < numArray.length; i++) { | |
added10.push(parseInt(numArray[i]) + 10); | |
} | |
return added10; | |
}; | |
var testArr = [1,2,'3',4,'5']; | |
var results =[]; | |
results.push(add10(testArr)); | |
results.push(add10b(testArr)); | |
results.push(add10c(testArr)); | |
results.unshift("**key** [11,12,13,14,15] for all"); | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick gist to prove the three ways (that I know of) to turn stringified numbers back into numbers in JavaScript.
This can be done using parseInt(stringifiedNum), Number(stringifiedNum), and +stringifiedNum.
See lines 10, 18, and 26 for usage