Software Development

Enter valueAsNumber

Enter valueAsNumber
Written by admin

Each every now and then I study a JavaScript property that I want I had identified about years earlier — valueAsNumber is one among them. The valueAsNumber supplies the worth of an enter[type=number] as a Quantity sort, as a substitute of the standard string illustration whenever 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!

  • Chris Coyier&#8217;s Favorite CodePen Demos

    David requested me if I would be up for a visitor publish choosing out a few of my favourite Pens from CodePen. A frightening activity! There are such a lot of! I managed to choose a number of although which have blown me away over the previous few months. When you…

  • Assign Anchor IDs Using MooTools 1.2

    One among my favourite makes use of of the MooTools JavaScript library is the SmoothScroll plugin. I apply it to my web site, my employer’s web site, and on many buyer web sites. The very best half concerning the plugin is that it is really easy to implement. I lately ran…


About the author

admin

Leave a Comment