To check whether a string string is null or empty in a coffee script

Asked 2 years ago, Updated 2 years ago, 89 views

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?

coffeescript javascript

2022-09-21 17:08

1 Answers

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)


2022-09-21 17:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.