Last active
June 14, 2023 14:07
-
-
Save KimSarabia/45a8dfde515c52c7940ee126a8df27eb to your computer and use it in GitHub Desktop.
ParseInt Tricky Use Case & Converting String of Numbers to Numbers
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
//From Map Article: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | |
// Consider: | |
['1', '2', '3'].map(parseInt); | |
// While one could expect [1, 2, 3] | |
// The actual result is [1, NaN, NaN] | |
// parseInt is often used with one argument, but takes two. | |
// The first is an expression and the second is the radix. | |
// To the callback function, Array.prototype.map passes 3 arguments: | |
// the element, the index, the array | |
// The third argument is ignored by parseInt, but not the second one, | |
// hence the possible confusion. See the blog post for more details | |
function returnInt(element) { | |
return parseInt(element, 10); | |
} | |
['1', '2', '3'].map(returnInt); // [1, 2, 3] | |
// Actual result is an array of numbers (as expected) | |
// Same as above, but using the concise arrow function syntax | |
['1', '2', '3'].map( str => parseInt(str) ); | |
// A simpler way to achieve the above, while avoiding the "gotcha": | |
['1', '2', '3'].map(Number); // [1, 2, 3] | |
// but unlike `parseInt` will also return a float or (resolved) exponential notation: | |
['1.1', '2.2e2', '3e300'].map(Number); // [1.1, 220, 3e+300] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks