0

I am using typescript for the first time, and I face a quite strange problem.

I am trying to make calculation like a calculator, so when a user press a button it could be a digit, 2 3 4 ... or an operation * \ + ...

On button press, I call a function, and the function check if it is a digit, or an operation in order to call the adequate function.

I compare my string like this :

if (Number(value) !== NaN) {
  // it is a number
} else {
  // it is an operation
}

although this work in the console ( by that I mean I do have a digit (3,4,5) when I press a number and NaN when I press an operation) this line ALWAYS return true, so I always consider it a number.

What am I doing wrong here?

thanks

Bobby
  • 4,372
  • 8
  • 47
  • 103

2 Answers2

3

To check is value is NaN or not you should use isNaN function.

if(isNaN(value)) {
  //do this if NaN
} else {
  //do this if not
}

BTW: On SO a lot of similar questions ;) eg: How do you test for NaN in JavaScript?

Alex Shtromberg
  • 740
  • 8
  • 21
0

Use isNaN(value). It returns true if value is a number and False otherwise.

Mean Coder
  • 304
  • 1
  • 12