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:
- Explanation: The string "JavaScript" is a non-empty string, so it evaluates to true.
2.Checking a String with a Space:
- Explanation: The string " " (a string containing a space) is also a non-empty string, which evaluates to true.
- Explanation: The number 100 is a non-zero number, so it evaluates to true.
- Explanation: The number -5 is also a non-zero number, making it truthy.
5.Checking an Empty Array & Object:
- Explanation: An empty array [] is considered truthy in JavaScript.
- Explanation: An empty object {} is also considered truthy
- 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