Convert String to Number in Javascript


/ Published in: JavaScript
Save to your folder(s)

Javascript sometimes confuses strings and numbers. When this happens, you get bizarre outcomes such as "2 + 2 = 22". The following code FORCES a conversion to a number.


Copy this code and paste it in your HTML
  1. var myval = Number(document.myform.mytext.value);
  2.  
  3. //var myval = document.myform.myval.value; myval++; myval--;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.