I need a little help with JavaScript

Hello.
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

Hello~!

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:

2 Likes

Hey there,

I think Google is worth a try:

https://www.google.com/search?hl=en&q=javascript+check+if+number

1 Like