DEV Community

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

Posted on

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

Top comments (0)