In this lesson, we'll discover truthy and falsy values in JavaScript. These values play a fundamental role in the decision-making structures found in code. Unique to programming, these concepts form the basis of how JavaScript and many other programming languages make decisions.
In JavaScript, a value can be considered 'truthy' or 'falsy'. If a value is interpreted as true
in a context where a boolean is expected, it is considered 'truthy'. Conversely, if it is interpreted as false
, it is considered 'falsy'. For example, 1
(the number one) is truthy, while 0
(the number zero) is falsy. Most values are considered truthy, but others like false
, 0
, ''
or ""
, null
, undefined
, and NaN
are inherently falsy.
Conditional statements give your code the ability to behave differently under various conditions. An "" statement, for instance, executes one block of code if the condition is truthy and another block if it's falsy. The anatomy of statement is shown below:
