1
value = "+"
isNaN(value)

Condition is false

value > 0

condition is true

value ="200"
isNaN(value)

Condition is false

value > 0

condition is true

Here's the thing, in both the cases value > 0 shows true, i want to differentiate things like +-/* with numbers 0,200,5 etc.

is it possible to do this in Javascript??

abc123
  • 17,855
  • 7
  • 52
  • 82
Venky
  • 1,929
  • 3
  • 21
  • 36
  • check if `value` is a number type beforehand, then, apply equality operator and `isNaN` function – RomanPerekhrest Jan 28 '17 at 07:40
  • 2
    `"+" > 0` = false in JavaScript. Also, if you need a way to check type then use `typeof`. See the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof). – Steven B. Jan 28 '17 at 07:46
  • define special character? I wouldn't consider + a special character – Bekim Bacaj Jan 28 '17 at 07:53
  • when value="+", the second condition is not true as mentioned in question! so it seems that we didn't need that `isNaN` check at all?! – S.Serpooshan Jan 28 '17 at 08:00
  • was trying to compare "+200" and that was causing the error,but thanks a lot for your input guys,really appreciate it,isNumeric thing worked :) and changed the logic to read "+ 200" instead of "+200" – Venky Jan 28 '17 at 14:18

1 Answers1

2

Seems to be working as you are wanting/expecting.

value = "+";
// is true as "+" isn't a number
console.log(isNaN(value));
// is false because "+" isn't greater than 0
console.log(value > 0);

value = "200";
// is false, because value is a number
console.log(isNaN(value));
// is true because "200" is greater than 0
console.log(value > 0);

However, you could implement a isNumeric function

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

value = "+";
// is false because "+" isn't numeric
console.log(isNumeric(value));
// is false because "+" isn't greater than 0
console.log(value > 0);

value = "200";
// is true, because value is numeric
console.log(isNumeric(value));
// is true, because "200" is greater than 0
console.log(value > 0);

Or you can use typeof

value = "+";
// is false as "+" is a string
console.log(typeof(value) === "number");
// is false because "+" isn't greater than 0
console.log(value > 0);

value = "200";
// is false, because value is a string not a number
console.log(typeof(value) === "number");
// is true because "200" is greater than 0, this auto converts to 200
console.log(value > 0);

Regex with numbers and symbols

function symbolCompare(value) {
  var results = /(\+|\-)(\d+)/.exec(value);
  if(results) {
    return results[2];
  }
  
  return false;
}

value = "+200";
// is false because 200 is a number
console.log(isNaN(symbolCompare(value)));
// is true because 200 > 0
console.log(symbolCompare(value) > 0);

value = "-200";
// is false because 200 is a number
console.log(isNaN(symbolCompare(value)));
// is true because 200 > 0
console.log(symbolCompare(value) > 0);
Community
  • 1
  • 1
abc123
  • 17,855
  • 7
  • 52
  • 82
  • Thanks a lot for your help,going to use the isNumeric thing :),i was trying to compare "+200" and that was causing the error – Venky Jan 28 '17 at 14:19
  • if you need to compare "+200" we'll want to use regex, something like what i added in the answer – abc123 Jan 30 '17 at 22:54
  • this simplifies many things,thanks a lot,i wrote a logic for detecting "+ 200" now this might come handy – Venky Feb 01 '17 at 04:48