Saturday, June 29, 2024
HomeSoftware DevelopmentEnter valueAsNumber

Enter valueAsNumber

[ad_1]

Each from time to time I find out about a JavaScript property that I want I had identified about years earlier — valueAsNumber is considered one of them. The valueAsNumber gives the worth of an enter[type=number] as a Quantity kind, as a substitute of the normal string illustration if you get the worth:

/*
 Assuming an <enter kind="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’s going to return NaN if the enter is empty.

Thanks to Steve Sewell for making me conscious of valueAsNumber!

  • Animated 3D Flipping Menu with CSS
  • Chris Coyier&#8217;s Favorite CodePen Demos

    David requested me if I might be up for a visitor submit choosing out a few of my favourite Pens from CodePen. A frightening process! There are such a lot of! I managed to choose just a few although which have blown me away over the previous few months. In the event you…


[ad_2]
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments