Tuesday, August 9, 2022
HomeSoftware DevelopmentEnter valueAsNumber

Enter valueAsNumber


Each occasionally I study a JavaScript property that I want I had recognized about years earlier — valueAsNumber is certainly one of them. The valueAsNumber offers 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!

Website performance monitoring
Website performance monitoring
Website performance monitoring
Website performance monitoring
  • 5 HTML5 APIs You Didn&#8217;t Know Existed

    5 HTML5 APIs You Didn’t Know Existed

    If you say or learn “HTML5”, you half anticipate unique dancers and unicorns to stroll into the room to the tune of “I am Attractive and I Know It.”  Are you able to blame us although?  We watched the elemental APIs stagnate for therefore lengthy {that a} primary characteristic…

  • Page Visibility API

    Web page Visibility API

    One occasion that is at all times been missing inside the doc is a sign for when the person is taking a look at a given tab, or one other tab. When does the person change off our website to have a look at one thing else? When do they arrive again?

  • Implementing Basic and Fancy Show/Hide in MooTools 1.2

    Implementing Primary and Fancy Present/Cover in MooTools 1.2

    One of many nice components of MooTools is that the library itself permits for max flexibility inside its supplied courses. You’ll be able to see proof of this within the “Class” class’ implement methodology. Utilizing the implement methodology, you’ll be able to add your personal strategies to…

  • Chris Coyier&#8217;s Favorite CodePen Demos

    Chris Coyier’s Favourite CodePen Demos

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


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular