We have compiled a code to compare whether the string is null or empty string with coffee script as below.
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}"
But
I wonder if there is a more concise way to write code than to write code with not email? or email is'
.
How do you usually check empty strings in coffee scripts?
Do it as below. The not operator converts it to boolean type."
or null
because the boolean value is false!If you do the math, it's true.
emailEmpty = !email
You can also check the length.
stringEmpty = (email, password) ->
not (email?.length and password?.length)
© 2024 OneMinuteCode. All rights reserved.