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
  • 10,209
  • 4
  • 56
  • 76
michael
  • 4,031
  • 5
  • 42
  • 67

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
  • 10,209
  • 4
  • 56
  • 76