Combining Conditions in JavaScript
JavaScript·2 min read·Jan 1, 2025
In JavaScript, when a non-boolean value is used in a boolean context, for example in a conditional statement, such as an if, this value is automatically coerced into either true or false to avoid unexpected behavior.
Any value that is considered true when evaluated in a boolean context is called truthy, and includes:
true- non-empty strings (e.g., "hello", "0", "false")
- non-zero numbers (e.g.,
1,-2.5) - arrays (even empty)
- objects (even empty)
- functions
On the other hand, any value that is considered false when evaluated in a boolean context is called falsy, and includes:
false0nullundefinedNaN- empty strings (e.g., "")
Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle