Friday, July 29, 2022
HomeSoftware developmentEnter valueAsNumber

Enter valueAsNumber

Each infrequently I find out about a JavaScript property that I want I had recognized about years earlier — valueAsNumber is one in every of them. The valueAsNumber supplies the worth of an enter[type=number] as a Quantity sort, as an alternative of the standard string illustration if you get the worth:

 Assuming an <enter sort="quantity" worth="1.234" />

// BAD: Get the worth and convert the quantity
enter.worth // "1.234"
const numberValue = parseFloat(enter.worth, 10);

// GOOD: Use valueAsNumber
enter.valueAsNumber // 1.234

This property permits us to keep away from parseInt/parseFloat, however one gotcha with valueAsNumber is that it’ll return NaN if the enter is empty.

Thanks to Steve Sewell for making me conscious of valueAsNumber!

  • 7 Essential JavaScript Functions

    I bear in mind the early days of JavaScript the place you wanted a easy perform for nearly all the pieces as a result of the browser distributors applied options otherwise, and never simply edge options, fundamental options, like addEventListener and attachEvent.  Occasions have modified however there are nonetheless a number of features every developer ought to…

  • Animated 3D Flipping Menu with CSS
  • Comment Preview Using MooTools

    Remark previewing is an superior addition to any weblog. I’ve seen actually easy remark previewing and a few actually complicated remark previewing. The next is a tutorial on creating very fundamental remark previewing utilizing MooTools. The XHTML You may arrange your XHTML any manner you want.

  • MooTools onLoad SmoothScrolling

    SmoothScroll is a incredible MooTools plugin however clean scrolling solely happens when the anchor is on the identical web page. Making SmoothScroll work throughout pages is as straightforward as a number of further line of MooTools and a querystring variable. The MooTools / PHP After all, it is a…



Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments