Easiest way to check if string is null or empty

前端 未结 11 1253
南笙
南笙 2021-01-30 12:30

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          


        
11条回答
  •  感情败类
    2021-01-30 12:58

    It isn't entirely equivalent, but email?.length will only be truthy if email is non-null and has a non-zero .length property. If you not this value the result should behave as you want for both strings and arrays.

    If email is null or doesn't have a .length, then email?.length will evaluate to null, which is falsey. If it does have a .length then this value will evaluate to its length, which will be falsey if it's empty.

    Your function could be implemented as:

    eitherStringEmpty = (email, password) ->
      not (email?.length and password?.length)
    

提交回复
热议问题