DEV Community

Cover image for Exploring the Core of true value vs false value in JavaScript...
Tanvir Ahmed
Tanvir Ahmed

Posted on

2

Exploring the Core of true value vs false value in JavaScript...

Truthy Value

In JavaScript, a truthy value is any value that is considered true when evaluated in a Boolean context. Values that are not falsy are considered truthy.

Example of truthy values:

  • Any non-zero number (1,-5,3.14)
  • Non-empty strings ("hello" )
  • space value(" ")
  • Objects ({}, [])
  • The boolean true

Now Example,

1.Checking a Non-empty String:
Image description

  • Explanation: The string "JavaScript" is a non-empty string, so it evaluates to true.

2.Checking a String with a Space:
Image description

  • Explanation: The string " " (a string containing a space) is also a non-empty string, which evaluates to true.

3.Checking a Positive Number:
Image description

  • Explanation: The number 100 is a non-zero number, so it evaluates to true.

4.Checking a Negative Number:
Image description

  • Explanation: The number -5 is also a non-zero number, making it truthy.

5.Checking an Empty Array & Object:
Image description

  • Explanation: An empty array [] is considered truthy in JavaScript.
  • Explanation: An empty object {} is also considered truthy

6.Checking the Boolean True:
Image description

  • Explanation: The boolean value true is, by definition, truthy.

Falsy Value

A falsy value is something which evaluates to FALSE, for instance when checking a variable.

Example of falsy values:

  • 0 zero && (-0) negative zero
  • empty strings ("" )
  • null,undefine & Nan
  • The boolean false

Now Example,

Part01
Image description
Part02
Image description

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

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

Okay