Last active
January 8, 2021 11:18
-
-
Save ahamed/8b1d8d67d42a1436feb55c422b6522f1 to your computer and use it in GitHub Desktop.
Converting string to number using JavaScript.
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
| /** | |
| * Ways to convert a numeric string to a number. | |
| * | |
| * @author Sajeeb Ahamed | |
| */ | |
| const str = '343'; | |
| // Using parseInt method. | |
| number = parseInt(str); | |
| // Using 0 bit right shifting. | |
| // Careful this only works with 32 bits numbers. | |
| number = str >> 0; | |
| // Using Number constructor | |
| // Note: For real number string it returns original real number | |
| number = Number(str); | |
| // Using unary plus (+) operator | |
| // Note: For real number string this also returns original real number | |
| number = +str; | |
| // Multiplying or dividing by 1 | |
| number = str * 1; // or | |
| number = str / 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment