Code Highlights logo

Learn

Conditional Statements

Truthy and Falsy

In programming, it is often necessary to evaluate the truthiness or falsiness of a value. This involves determining whether a value is considered "truthy" or "falsy". Understanding truthy and falsy values is crucial when working with conditional statements.

Every value has an inherent boolean value: either true or false. However, there are certain values that are considered falsy, meaning they evaluate to false when used in a boolean context. On the other hand, there are truthy values that evaluate to true in a boolean context.

The following values are considered falsy:

  • false
  • 0
  • empty string ("" or '')
  • null
  • undefined
  • NaN (Not-a-Number)

All other values in JavaScript are considered truthy.

Example:

1let name = ''; // an empty string
2
3if (name) {
4console.log('Name is truthy.');
5} else {
6console.log('Name is falsy.');
7}

Output:

1Name is falsy.

In the above example, even though the variable name is assigned an empty string, it is considered falsy. Thus, the condition if (name) evaluates to false, and the code within the else block is executed.


Instructions

1.

Change the value of age so that it is truthy. This value should still be a number.
After you make this change and run your code, 'Age is truthy.' should log to the console.

2.

Change the value of movie so that it is still a string but falsy.
After you make this change and run your code, 'Movie is falsy.' should log to the console.

Sign up to start coding

Already have an account?

Sign In

Course content