DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on

1 1 1 1 1

What is Truthy and Falsy in Javascript ?

In JavaScript, "truthy" and "falsy" are terms used to describe values that are treated as true or false in certain situations.

A truthy value is something that JavaScript sees as true when it needs to make a decision. If JavaScript was a person and you asked it, "Is this thing true?" and the thing was a truthy value, JavaScript would say, "Yes."

A falsy value is the opposite. It's something that JavaScript sees as false when it needs to make a decision. If you asked JavaScript, "Is this thing true?" and the thing was a falsy value, JavaScript would say, "No."

Here are some examples:

Falsy values (these are seen as false):

false itself
0 (the number zero)
"" (an empty string)
null (a special value meaning "nothing")
undefined (a special value meaning "this thing doesn't exist")
NaN (a special value meaning "Not a Number")
Enter fullscreen mode Exit fullscreen mode

Everything else in JavaScript is truthy. That means if it's not in the list above, JavaScript thinks of it as true. Some examples are:

Truthy values (these are seen as true):

Any number that isn't zero (like 1, -1, 100, 0.5, etc.)
Any non-empty string (like "hello", "false", "0", etc.)
Arrays (like [] or [1, 2, 3])
Objects (like {} or {name: 'John', age: 30})
Enter fullscreen mode Exit fullscreen mode

Remember, these values are only treated as true or false when JavaScript needs to make a decision, like in an if statement. In other situations, these values are just themselves.

Thank you for reading, please follow me on Twitter, i regularly share content about Javascript, and React and contribute to Opensource Projects

Twitter-https://twitter.com/Diwakar_766

Github-https://github.com/DIWAKARKASHYAP

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay