I need a little help with JavaScript

I wanted to make a unit converter in JS but I stumbled across a problem with my if statement. The problem is that I wanted to make sure that whenever anything else than a number is entered, the result will be a string saying “please enter a number” but JS recognizes everything that is being typed as if it was a string.

if(typeof meter.value != 'number'){
    km.innerText = "please enter a number"
  } else {
km.innerText = meter.value / 1000}

And here’s the whole code: https://codepen.io/VeljkoCukic/pen/qBZNBJp


You are grabbing the meter.value from an HTML <input> element, which JavaScript will always read as a string. You have a few options for turning that string into a number.

As a hint, though, when you apply those methods, if JavaScript cannot make the string into a number it will give you NaN. Which… is also a number. :slight_smile:


Hey there,

I think Google is worth a try:


1 Like