1

I get a boolean return when checking for a falsy value, but not a boolean when checking for a truthy value... I am just getting a string instead.

This works.

const isFalse = !values.firstName && !values.lastName && !values.email;

But this doesn't, as I get returned a string only.

const isValid = values.firstName && values.lastName && values.email;

Although, I can achieve the desired result with the following... It doesn't seem the best way forward.

const isValid = values.firstName && values.lastName && values.email ? true : false;

How would I be able to check the string for a truthy value and get boolean as a result (instead of a string)?

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • 1
    Does this answer your question? [Why don't logical operators (&& and ||) always return a boolean result?](https://stackoverflow.com/questions/5417969/why-dont-logical-operators-and-always-return-a-boolean-result) (Check the highest voted answer, _not_ the accepted answer.) – Ivar Sep 14 '20 at 11:35
  • @Ivar Not quite mate. – Grateful Sep 14 '20 at 11:37
  • 1
    `!` does not turn a truthy value into a falsy value, it turns a truthy value into `false` and a falsy value into `true` – Jonas Wilms Sep 14 '20 at 11:38

2 Answers2

1

Wow, that was quick. I found out that you can do a double negative to achieve the same result and get it to work!! (pun intended)

const isValid = !!(values.firstName && values.lastName && values.email);
Grateful
  • 9,685
  • 10
  • 45
  • 77
  • While this is correct, it does not answer **Why does this javascript condition return a boolean when checking for a falsy string value, but return a string when checking for a truthy value?**. – Lain Sep 14 '20 at 12:10
  • I've edited the question to reflect what I was actually looking for as an answer. Thank you nonetheless. – Grateful Sep 14 '20 at 12:19
  • I see. What you basically want to do is `values.every((v) => v)` or `['g', 'r', '@'].every((v) => v)`. [every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every). – Lain Sep 14 '20 at 12:27
  • @Lain Umm... Thank you for sharing about 'every'... But it is an overkill for what I need. So the two negatives will suffice, thank you. – Grateful Sep 14 '20 at 12:29
-2

Well, Javascript can be weird sometimes... I don't know exactly why it happens, but I can tell you how to get a boolean value:

const isValid = !!values.firstName && !!values.lastName && !!values.email;

BONUS: for a good laugh, check this: https://www.destroyallsoftware.com/talks/wat

xurei
  • 1,057
  • 8
  • 22
  • Thank you for your participation. I chanced upon the answer in parallel. – Grateful Sep 14 '20 at 11:40
  • Well apparently someone doesn't like this answer... – xurei Sep 14 '20 at 11:42
  • I am sorry about that buddy, but it wasn't me. Someone apparently doesn't like my question either. :) – Grateful Sep 14 '20 at 11:44
  • @Grateful Uhh, your question has an upvote (and no downvotes)? – Ivar Sep 14 '20 at 11:45
  • @xurei Beats me... They just closed the question. In any case, with "privileged trigger-happy people", the place can be weird sometimes. Over the years, I have learnt to take what I need and get outta here. :) – Grateful Sep 14 '20 at 11:47
  • @Grateful yeah this is sad. I probably should have flagged your question as a duplicate. Not very welcoming IMO. Thanks for the upvote, Karma is balanced again ^_^ – xurei Sep 14 '20 at 11:51
  • @xurei Although, the downvotes are probably due to the unrelated "bonus" that you provided towards the end. – Grateful Sep 14 '20 at 12:34