Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The valueAsNumber property of the HTMLInputElement interface represents the current value of the <input> element as a number or NaN if converting to a numeric value is not possible.
This property can also be set directly, for example to set a default numeric value based on some condition.
A number that represents the value of the element or NaN if numeric conversion is impossible.
In this example, the log displays the current value of the number input when changed.
We include an <input> of type number and an associated <label>, with a <pre> container for our output.
The <pre> element's innerText is updated to the current value of the <input> every time a change event is fired.
If you delete the number in the widget, the result is NaN.
This example demonstrates the valueAsNumber property of an <input> with type datetime-local.
We include an <input> of type datetime-local:
When no date or time is selected, the empty string resolves to NaN. Each time a selection is made, a change event is fired, updating the <pre> content showing the HTMLInputElement.value of the form control compared to that value as a number.
| HTML # dom-input-valueasnumber-dev |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Aug 23, 2024 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.