1

I have variable named objectId.The objectId contains some string number.

In this row I try to check wether some value exists in objectId:

   self.showObjects = self.objectId == true ? true : false;

But even if value exists the showObjects get false.

Any idea how to fix the row to make ot work properly?

Sushanth --
  • 54,565
  • 8
  • 62
  • 98
Michael
  • 12,788
  • 49
  • 133
  • 253

1 Answers1

4

You could use

self.showObjects = !!self.objectId;

The use of double not will coerce the value to a boolean, which will always get converted to truthy or falsy values.

!!0 
!!""
!!false
!!NaN

All operations return false.

Any other non empty value will give you true.

Sushanth --
  • 54,565
  • 8
  • 62
  • 98
  • You can simply link to http://stackoverflow.com/q/784929/1048572. No need to explain everything again here :-) – Bergi Oct 06 '16 at 19:05