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 --
  • 55,259
  • 9
  • 66
  • 105
Michael
  • 13,950
  • 57
  • 145
  • 288

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 --
  • 55,259
  • 9
  • 66
  • 105
  • 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