Created
June 3, 2020 14:47
-
-
Save bobdobbalina/73fcc7ed46c2053020b3b63a46e74ebf to your computer and use it in GitHub Desktop.
Javascript: String and Number Cohersion
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
| // NUMBER TO STRING | |
| //========================== | |
| const val = 1 + ''; | |
| console.log( val ); // Result: "1" | |
| console.log( typeof val ); // Result: "string" | |
| // STRING TO NUMBER | |
| //========================== | |
| let int = '15'; | |
| int = +int; | |
| console.log( int ); // Result: 15 | |
| console.log( typeof int ); Result: 'number'; | |
| // IN ARRAYS | |
| let selected_values = [ '1', '5', '8' ]; | |
| selected_values = selected_values.map( Number ); // [ 1, 5, 8 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment