-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

3 Answers3

0

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

!0 // is true
!$.isNumeric(0) // is false
mjsarfatti
  • 1,615
  • 1
  • 15
  • 21
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,015
  • 12
  • 42
  • 57
-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
  • 25,247
  • 27
  • 110
  • 174
  • 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