1

I have below jquery if\else comparison to evaluate part of code based on results,

if($('#order_quantity').val() < $('#available_quantity').val()) {
   // ---code to run----
} else {
   // --- code to show error---
}

This code work 3-4 times correctly for inputs and after that it working wrong,

when $('#order_quantity').val() = 5 and $('#available_quantity').val() = 46, then ideally --code to run-- should execute but it is running --code to show error -- of else statement.

Sometimes it correct and sometimes it not, I created console.log for above input values, inputs are correct but still comparison results throw wrong results

What can be problem in this comparison?

Tushar
  • 85,780
  • 21
  • 159
  • 179
rjcode
  • 1,193
  • 1
  • 25
  • 56

5 Answers5

6

The values read from the DOM are strings. You need to convert it to numbers in order to use it in arithmetic operation unless the operator coerces the operand to number.

if(+$('#order_quantity').val() < +$('#available_quantity').val())

You can convert string to number using

  1. By preceding the string with unary + operator, this will coerce the string to Number.
  2. By using Number function
  3. Using parseFloat
  4. Using parseInt, but this will convert the float value to integer.

this code work 3-4 times correctly for inputs and after that it working wrong

The code works for some values because how the strings are compared.

> "5" < "46" // false

The strings are compared character by character just like how sorting works. In this code the comparison is done as '5' < '4', when comparing the strings their ASCII values is compared. As the ASCII value of 5 is greater than that of 4 the comparison returns false.

Tushar
  • 85,780
  • 21
  • 159
  • 179
5

val() returns a string. You have to convert these string values in numbers:

if(Number($('#order_quantity').val()) < Number($('#available_quantity').val())) {
     // do something
} else {
     // do something
}

An example when your expression fails:

"24" < "124"
false

There are other ways to convert strings into numbers (see What's the fastest way to convert String to Number in JavaScript?):

  • Number(x)
  • parseInt(x, 10) (for integers)
  • parseFloat(x)
  • +x;
  • ~~x (for integers)
  • x | 0 (for integers)

...or using Math methods. x is the string value.

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • yeah, `"1" < "2"` returns `true`. But, `"2" < "112"` returns `false` – Sherali Turdiyev Oct 04 '15 at 07:43
  • 1
    @SheraliTurdiyev That's because that is the behaviour of strings. It is compared using the character by character basis. So in the second example is translated as `'2' < '1'` and as the ASCII value of 2 is greater than that of 1, the condition returns `false`. Hope that makes sense. This problem is also observed when sorting the numbers, there also you've to explicitly convert the string to number before comparing – Tushar Oct 04 '15 at 07:45
  • @SheraliTurdiyev Yes, for the same reason why `"a" < "b"` is `true`. – Ionică Bizău Oct 04 '15 at 07:45
2

You will need to convert string to integer as all values read with jQuery val() method are returned as strings.

You can also use this code -

if(parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))
Neha
  • 673
  • 9
  • 16
2

The type of $('#order_quantity').val() is string. '5' > '46' is true just like 'b' > 'a' is true. To convert string value to int value, you can use parseInt function. To get the intended result your code should be something like:

if (parseInt($('#order_quantity').val()) < parseInt($('#available_quantity').val()))
  {
    //---code to run----
  }
else
  {
    //--- code to show error---
  }
ryubro
  • 338
  • 1
  • 8
2

Try these solution :

var order_quantity =  $('#order_quantity').val();
var available_quantity = $('#available_quantity').val();

if( Number( order_quantity ) < Number( available_quantity ) )
{ //---code to run---- }
else
{ //--- code to show error---}

Number

Or

if (order_quantity - available_quantity  < 0)
{ //---code to run---- }
else
{ //--- code to show error---}

This forces javascript to process a numeric calculation. Strings will be converted into numbers.

Sherali Turdiyev
  • 1,745
  • 16
  • 29
Webloper
  • 178
  • 1
  • 5
  • 15