Code Editor
Control Flow

True and False Values II

In programming, we often evaluate whether or not an expression is true or truthy. Conveniently, JavaScript provides a shorthand notation for this.

let isRaining = true; if (isRaining) { console.log('Carry an umbrella!'); } else { console.log('Enjoy the sun!'); }

In the example above, the condition is simply if (isRaining). In JavaScript, this is evaluating whether isRaining is truthy. If you read the code out loud to yourself, it sounds like a simple sentence: "If it's raining, carry an umbrella. Else, enjoy the sun!"

JavaScript provides an operator for swapping the truthiness and falsiness of values - the exclamation point (!). We can use this in conditional statements as shorthand to check if the value of a variable evaluates to false rather than true.

let isPhoneCharged = true; if (!isPhoneCharged) { console.log('Plug in your phone!'); } else { console.log('No need to charge!'); }

In the example above, the program checks if isPhoneCharged evaluates to false. Because isPhoneCharged is true, the second block of code will execute.

Report a Bug
If you see a bug or any other issue with this page, please report it here.