jcollum jcollum - 1 year ago 117
CoffeeScript Question

Easiest way to check if string is null or empty

I've got this code that checks for the empty or null string. It's working in testing.

eitherStringEmpty= (email, password) ->
emailEmpty = not email? or email is ''
passwordEmpty = not password? or password is ''
eitherEmpty = emailEmpty || passwordEmpty

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"

What I'm wondering is if there's a better way than
not email? or email is ''
. Can I do the equivalent of C#
in CoffeeScript with a single call? I could always define a function for it (like I did) but I'm wondering if there's something in the language that I'm missing.

Answer Source


passwordNotEmpty = not not password

or shorter:

passwordNotEmpty = !!password
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download