Javascript – Number function

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. It can also be used to detect if a String is valid number.

Example – Number() returning valid number

<script>
  document.write(Number("123") + "<br>");
  document.write(Number("12.3") + "<br>");
  document.write(Number("") + "<br>");
  document.write(Number("0x11") + "<br>"); // hex
  document.write(Number("0o11") + "<br>");  // octal
</script>
refresh

Example – Number() returning NaN

<script>
  document.write(Number("foo") + "<br>");
  document.write(Number("100a") + "<br>");
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments