JavaScript Falsy Explanation

Can someone please explain the concept of falsy to me? I’m a beginner and I’m having a lot of trouble understanding it. You’ll have to explain it to me like I’m an 8 year old. Please don’t post any external resources as I’m hoping someone can explain it to me here via a conversation about it. I’ve also read and watched a lot about it and the online explanations are over my head. Thank you.

Good morning.

The simplest definition I know of for falsy is by example.

A variable is falsy if the variable evaluates to false when used in an if statement:

let myVar = 0;

if (myVar) {
  console.log("This variable is not falsy");
} else {
  console.log("This variable is falsy");
// This code will print "This variable is falsy"

This is in contrast to truthy variables, which evaluate to true in an if statement:

let myVar = "banana";

if (myVar) {
  console.log("This variable is truthy");
} else {
  console.log("This variable is not truthy");
// This code will print "This variable is truthy"

You can try these examples in your browser console, in the Free Code Camp editor, or in this link.

In JavaScript there is a kind of values called “boolean”, these values are just two: true and false

Any other value in javascript can be described as true-like or false-like, meaning that in certain situations behave exactly like the corresponding boolean.
The true-like values are called “truthy”, the false-like values are called “falsy”

They behave exactly like the corresponding booleans, and you can use this for example in if statement conditions, or ternary operators, like shown by @JeremyLT above

1 Like

I’m trying to get a fresh start. Can you please delete your post and refrain from posting on this thread? Thank you.

I understand that something can be true or false. What do you mean by “behave…like a boolean?”

the truthy values behave like the boolean value true and the falsy values behave like the boolean value false, if you look at the post by @JeremyLT he shows that behaviour: put a falsy value in the condition of an if statement, and that if statement will not execute, put a truthy value in the condition of an if statement, and that if statement will execute

I’m not looking at any post by jermeylt.

I interpret what you say as if something is true, it will return true and if false, false. Is that right?

a falsy value is not equal to false, but it behaves like it when evaluated as a boolean
same thing for a truthy value

try it yourself, see what happens

I don’t understand what you are saying here. “behaves…as a boolean” Please pretend I’m 8 years old. Think of how you’d literally try to explain this concept to an 8 year old.

do you understand what a boolean is?

Something that is true or false.

Here we are not talking about the logic concept of true or false, we are talking about specific values, true and false, which are values of type boolean
they behave in a specific way, and many operators and methods give back a boolean (true or false) when used

If they are values, I would interpret them as strings.

If they are conditions, I would interpret them as Boolean.

strings and booleans are both type of values in javascript, if one thing is a boolean it can’t be a string, and if it’s a string it can’t be a boolean: the boolean true is completely different from the string `“true”

I’m sorry, but it seems you need to review some basics, if you don’t clear these dubts you would not be able to solve the algotihms

That’s what I’m explicitly stating above. If it’s a value like “true,” that’s a string but if it’s a condition, that’s a boolean.

and what happens if you put a string in the condition of an if statement?

Can you be more specific? As far as I know, strings are allowed in if statements.

if ("false") {
   // is this executed?