0

Is it acceptable to just test for "truthy" in coffeescript? I'm looking for a best practice to soak up empty strings in object attributes.

Given:

obj = {
  "isNull": null,
  "isEmptyString": "",
  "isZero": 0
}

## coffeescript
# obj.isNull?         === true
# obj.isEmptyString?  === false
# obj.isZero?         === false

Which is safer or preferable??

# obj.isEmptyString    ==  "truthy"    
# !!obj.isEmptyString  === true
Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81
michael
  • 4,377
  • 8
  • 47
  • 73

1 Answers1

1

I believe !! is the accepted method:

obj = {"isNull": null, "isEmptyString": "", "isZero": 0, "isValue": 1}
!!obj.isNull # false
!!obj.isEmptyString # false
!!obj.isZero # false
!!obj.isValue # true

EDIT: Possible duplicate: Easiest way to check if string is null or empty

Community
  • 1
  • 1
Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81