Skip to content

Instantly share code, notes, and snippets.

@bobdobbalina
Created June 3, 2020 14:47
Show Gist options
  • Select an option

  • Save bobdobbalina/73fcc7ed46c2053020b3b63a46e74ebf to your computer and use it in GitHub Desktop.

Select an option

Save bobdobbalina/73fcc7ed46c2053020b3b63a46e74ebf to your computer and use it in GitHub Desktop.
Javascript: String and Number Cohersion
// 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