What are falsy values in JS? What are truthy values in JS?

// original code
const truthyOrFalsy = (value) => {
if (value) {
return 'True';
}
return 'False';
}

@mtf I was actually wondering about that… in what kinds of scenarios would we write return true versus return "True"? When I write the booleans with and without strings, they both seem to work…

// rewritten with strings function truthyOrFalsy (value) { if (value) { return 'true'; } else { return 'false' } } console.log(truthyOrFalsy());
// rewritten without strings function truthyOrFalsy (value) { if (value) { return true; } else { return false; } } console.log(truthyOrFalsy());
1 Like

One cannot think of a scenario that isn’t by design, as output for the user to see. In code we only use primitives, or the outcomes of logical statements or expressions.

Ah okay… I think I see your point ^U^ After all, it is simpler and more straightforward to write true than enclosing it in quotes, when we mean to do the same thing. Thank you for the quick response!

1 Like

Thank you so much for telling me this! I can write functions, but I seem to either over or under complicate them for these exercises! And I never seem to guess how they want me to write these functions! Do you have any advice for how to read and write these instructions for me to get them right the first time when I run the code? And not having to either check the hint or the forum? :frowning:

1 Like

Exactly!!! I wrote the 1st one and it was giving me an error! Why? It returned false! :frowning:

Until such time as you can formulate your own ideas and have them play out as you expect, do rely upon the hints and the forums. It’s not a crime or a sin. We all struggle with new concepts. That’s why practice and repetition are so important. Grab anything and everything that will help cement concepts in your mind.

1 Like

Thank you for saying this! But isn’t looking at the hints before finishing the exercise considered cheating?

No, far from it. When faced with a new thing we all need a starting point, or hint of where to begin. Relish in them, and use without guilt. They’re there to help.

1 Like

Thank you! I want to try and test my logic, but I finally found my weak point in programming. Not knowing how to transform exercise requirements into code without hints or instructions. How can I increase my logic to know exactly what to and how to implement when I read requirements? :’(

1 Like

A lot is rote learning. Logic is also built on that. What are the requirements?

Well, for example this problem:

Write a function, howOld() , that has two number parameters, age and year , and returns how old someone who is currently that age was (or will be) during that year . Handle three different cases:

  • If the year is in the future, you should return a string in the following format:
'You will be [calculated age] in the year [year passed in]'
  • If the year is before they were born, you should return a string in the following format:
'The year [year passed in] was [calculated number of years] years before you were born'
  • If the year is in the past but not before the person was born, you should return a string in the following format:
'You were [calculated age] in the year [year passed in]'

It makes no sense, except for the fact that I need to write a function called howOld() with the 2 parameters and based on what it says, I am sure I need if statements if a certain age is reached. But how to put the pieces together is what I don’t understand. Can you please tell me how do you come up with what needs to be done for problems like these? Do you have a process? Thank you!

Yes. It starts with understanding the problem.

1 Like

And what do you do to understand the problem when struggling to understand?

I solved it like following, but not sure if all type are covered:

const truthyOrFalsy = value => value ? value = true : value = false;

I did this as well.

Didn’t see that explanation mtf asked for.

// function expression with a variable tf that returns !!tf
// first ! inverts the equated boolean return as literal boolean true/false.
// second ! inverts the first inverted boolean to get starting true/false literal boolean.
const truthyOrFalsy = tf => !!tf;

MDN explains it better than I can hence the link to “Double NOT (!!)” (above).

1 Like

I really struggled with this one too. I knew right off the bat this function would require some math and I am REAL bad at math haha. I ended up looking up the answer and then tried to understand how they got that answer with the information that was given and I honestly still don’t have any idea.

If anyone else has a solution to this sort of thing I’d appreciate an answer as well.

1 Like

I tried the following:

const truthyOrFalsy = value => value ? true : false;

console.log(truthyOrFalsy(42)); // prints 'true', which is correct *1

const truthyOrFalsyWrongVariant = value => value == true ? true : false;

console.log(truthyOrFalsyWrongVariant(42)); // prints 'false', which is wrong *1

*1: According to https://developer.mozilla.org/en-US/docs/Glossary/Truthy

I don’t understand why truthyOrFalsy(42) and truthyOrFalsyWrongVariant(42) don’t lead to the same result. Perhaps someone can explain the weird behavior?

That is a comparison of identity. 42 != true => true

That is a test of truthiness. Nothing is being equated, only evaluated.

1 Like

Thank you very much for your immediate, concise reply!

Sometimes it’s like having a board in front of one’s head…

1 Like