Write a clean convertToNumber function, if value is a number
One problem with Number(value); is that it casts falsy values as 0 and truthy values as 1.
That means that an empty string becomes 0, which may not be what you expect.
You could make a regular expression test on the input to determine if it fits your expectations for a number.


let input = document.getElementById('input');
let result = document.getElementById('result');

function IsANumber() {
  let isItANumber = input.value.match(/^-?\d*\.?\d+$/);
  result.innerHTML = isItANumber ? 'true' : 'false';
}
&lt;input id="input" onchange="IsANumber()" onkeyup="IsANumber()" /&gt;&lt;br/&gt; Is a number: &lt;span id="result"&gt;&lt;/span&gt;



The example above will not fail in truthy/falsy cases, but it will determine strings like &quot;Infinity&quot; to not be numbers too.
Therefore it may need to be modified if you have to parse these to numeric values.