-4

I have a condition that needs to be validated with a string on one side and a decimal in the other side. However if the second condition is null this should return true, to enter and display my is required message.

if (!equipObject[i].Mass[0].massId  && !equipObject[i].Mass[0].Price ) {
    // display message - either massId or Price are required!
}

however I am thinking about changing the second condition

!equipObject[i].Mass[0].Price

for

!$.isNumeric(equipObject[i].Mass[0].Price)

How are these two validations different? which one is optimal? 0 is not a valid price according to the BL.

X10
  • 21
  • 4
  • They behave differently for values of `0` and all values that are not numbers. So which to use depends on your use case. – str Aug 04 '16 at 12:56
  • 1
    The first one converts whatever is in the `Price` property and coerces it to boolean. This could cause issues where a valid value is coerced incorrectly - eg `0` would be `false`, yet `0` may be valid for your logic. – Rory McCrossan Aug 04 '16 at 12:56
  • don't think so.. 0 is a number – Gogol Aug 04 '16 at 12:57
  • @SugatoSengupta Who are you responding to? – str Aug 04 '16 at 12:58
  • To OP. Sorry for not mentioning lol. – Gogol Aug 04 '16 at 12:59
  • 1
    `!value` will be satisfied for all falsey conditions. `!$.isNumeric` will satisfy for all non numeric value. Both are very different. – Rajesh Aug 04 '16 at 12:59

3 Answers3

0

If equipObject[i].Mass[0].Price is 0:

!0 // is true
!$.isNumeric(0) // is false
mjsarfatti
  • 1,725
  • 1
  • 15
  • 22
0

OK, CRec,

In this case !equipObject[i].Mass[0].Price you are testing the object, if they exist and has value.

In this case !$.isNumeric(equipObject[i].Mass[0].Price. you are testing if the value are a numeric valor.

If you need to confirm the price is always numeric, the second one is the best, you are guaranteed you have a price higher than 0 in the variable.

George G
  • 7,443
  • 12
  • 45
  • 59
-2

TL;DR

They're very different and do essentially totally different things


!equipObject[i].Mass[0].Price

Here you take a "value" and it's coerced into a true/false value and then a "not" is applied. So if we replace equipObject[i].Mass[0].Price with actual values you get the following results:

!0 //true
!1 //false
!2 //false
!0.00 //true
!1.8884441 //false
!"1.8884441" //false
!"one" //false
!true //false
!false //true
!null //true

Etc. So this isn't very type safe.

!$.isNumeric(equipObject[i].Mass[0].Price)

this utilises jQuery's isNumeric function.

The $.isNumeric() method checks whether its argument represents a numeric value. If so, it returns true. Otherwise it returns false. The argument can be of any type.

So let's do the same:

!$.isNumeric(0) //false
!$.isNumeric(1) //false
!$.isNumeric(2) //false
!$.isNumeric(0.00) //false
!$.isNumeric(1.8884441) //false
!$.isNumeric("1.8884441") //false
!$.isNumeric("one") //true
!$.isNumeric(true) //true
!$.isNumeric(false) //true
!$.isNumeric(null) //true

so very different results.

which one is optimal?

That depends on your definition of optimal. I'm presuming you actually want to check if something is an integer and greater than zero, in which case I think you need a third way:

!$.isNumeric(equipObject[i].Mass[0].Price) && equipObject[i].Mass[0].Price !== 0

notice the !==. This prevents coercion

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
  • actually my value is a decimal( 0.00 ), and yes my validation should exclude that 0 as a invalid price( BL decision). – X10 Aug 04 '16 at 14:02
  • I think this would do the trick: !$.isNumeric(equipObject[i].Mass[0].Price) || equipObject[i].Mass[0].Price !== 0, since I want to display the message if either the value is null(the object is null) or the value is 0, since is not a valid price according to my business logic. – X10 Aug 04 '16 at 14:08
  • Added some more examples to illustrate. It's pretty easy to test yourself: [try it here](https://jsfiddle.net/ht7uuvn7/) – Liam Aug 04 '16 at 14:12
  • actually your presumption was right was what I was looking. good answer and good examples. – X10 Aug 04 '16 at 14:56
  • Can anyone explain the -1? – Liam Aug 08 '16 at 07:52