Last active
October 27, 2022 21:53
-
-
Save tevashov/5172441 to your computer and use it in GitHub Desktop.
How to force a string to act as a number #JS
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
| var foo = 1; | |
| var bar = '2'; | |
| console.log(foo + bar); // 12. uh oh | |
| // coerce the string to a number (see also parseInt()) | |
| console.log(foo + Number(bar)); | |
| // Forcing a string to act as a number (using the unary-plus operator) | |
| console.log(foo + +bar); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment