Monday, November 28, 2022
HomeSoftware DevelopmentEnter valueAsNumber

Enter valueAsNumber


Each occasionally 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 sort, as a substitute of the normal 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’s going to return NaN if the enter is empty.

Thanks to Steve Sewell for making me conscious of valueAsNumber!

  • Tips for Starting with Bitcoin and Cryptocurrencies

    Probably the most rewarding experiences of my life, each financially and logically, has been shopping for and managing cryptocurrencies like Bitcoin, Litecoin, Ethereum. Like studying every other new tech, I made rookie errors alongside the way in which, however discovered some greatest practices alongside the way in which. Take a look at…

  • Animated 3D Flipping Menu with CSS
  • Create a NoScript Compatible Select Form Element with an onChange Event
  • Ana Tudor&#8217;s Favorite CodePen Demos

    Cocoon I like canvas, I like interactive demos and I do not suppose I’ve ever been extra impressed by any individual’s work than once I found what Tiffany Rayside has created on CodePen. So I needed to begin off with considered one of her interactive canvas pens, regardless that…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments