Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLInputElement.valueAsNumber — an input's numeric value

Read and write an input's numeric value. Recognize NaN and remember that conversion depends on the input type.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

input.valueAsNumber
input.valueAsNumber = number

HTMLInputElement.valueAsNumber reads an input's value as a number, whereas value holds text. This is an HTML Standard browser API, not an ECMAScript feature.

An empty number input, failed conversion, or unsupported type returns NaN. Test with Number.isNaN(). Assigning NaN to valueAsNumber clears a number input, as the example shows. The range type also supports valueAsNumber, but its value sanitization rules replace the empty string with a numeric default. Assigning infinity throws TypeError, and assigning a finite number to an unsupported type throws InvalidStateError.

Conversion depends on type: date and time types have their own numeric representations. A numeric result does not guarantee compliance with min, max, or step; check validation separately.

The example uses a detached number input. It requires a browser DOM; Node 24 alone does not establish support.

Examples

const input = document.createElement('input');
input.type = 'number';
input.step = 'any';
input.value = '12.5';
console.log(input.valueAsNumber + 2);

input.valueAsNumber = 7;
console.log(JSON.stringify(input.value));

input.valueAsNumber = NaN;
console.log(JSON.stringify(input.value));
console.log(Number.isNaN(input.valueAsNumber));

Result

14.5
"7"
""
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources