Skip to content

Instantly share code, notes, and snippets.

@tevashov
Last active October 27, 2022 21:53
Show Gist options
  • Select an option

  • Save tevashov/5172441 to your computer and use it in GitHub Desktop.

Select an option

Save tevashov/5172441 to your computer and use it in GitHub Desktop.
How to force a string to act as a number #JS
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