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 "if-else" statement, for instance, executes one block of code if the condition is truthy and another block if it's falsy. The anatomy of if-else
statement is shown below:
JavaScript1// Minimal example 2if (condition) { 3 // Code to execute if the condition is true 4} 5 6// An example of standard if-else block 7if (condition) { 8 // Code to execute if the condition is true 9} else { 10 // Code to execute if all the above conditions are false 11} 12 13// An example of extended if-else block 14if (condition) { 15 // Code to execute if the condition is true 16} else if (anotherCondition) { 17 // Code to execute if the first condition is false and the second condition is true 18} else { 19 // Code to execute if all the above conditions are false 20}
In this if
statement, the code inside the {}
is the block. It only runs if the condition is true, similarly the else
branch has its block which runs if the condition is false. In JavaScript, a block refers to a group of code enclosed within curly braces {}
. Think of a block like a container that holds related code together. Blocks are important in JavaScript because they define the scope of different code statements, which means they control where certain variables and functions can be accessed and used in your code.
Let's consider an example we all could relate to, an online test score updater:
JavaScript1let score = 85; 2 3if (score > 50) { // Check if the score is greater than 50 4 console.log('Congratulations, you passed!'); 5} else { 6 console.log('Sorry, you failed. Better luck next time.'); 7}
If the test score is more than 50, 'Congratulations, you passed!' is printed. Otherwise, it prints 'Sorry, you failed. Better luck next time.'.
To continue this lesson, let's review equality operations. These will assist us in comparing values and expressions. We often use ==
and ===
, which represent equality and strict equality, respectively, as well as <
, >
, <=
, and >=
for comparisons. This is akin to comparing the weights of apples to select the heaviest one:
JavaScript1console.log(1 == "1"); // Checks if 1 is equal to "1" in value. Outputs: true 2console.log(1 === "1"); // Checks if 1 is exactly equal to "1" in value and type. Outputs: false 3console.log(1 > 0); // Checks if 1 is greater than 0. Outputs: true 4console.log(1 < 0); // Checks if 1 is less than 0. Outputs: false 5console.log(1 >= 1); // Checks if 1 is greater than or equal to 1. Outputs: true 6console.log(1 <= 0); // Checks if 1 is less than or equal to 0. Outputs: false 7console.log(1 !== 0); // Checks if 1 is not equal to 0. Outputs: true – The '!' in '!==', signifies 'NOT', indicating inequality.
When multiple conditions must be considered simultaneously, JavaScript provides the perfect tools: logical operators. We have AND (&&
), OR (||
), and NOT (!
).
The AND operator (&&
) behaves like the word 'and'. It tests whether both of its conditions are true. This is akin to checking if you have apples and if a bakery is open to determine if you can make apple pie:
JavaScript1let hasApples = true; 2let bakeryOpen = true; 3 4if(hasApples && bakeryOpen){ 5 console.log("We can make apple pie!"); // This will output: "We can make apple pie!" 6}
However, if you're fine with making either an apple pie or a banana smoothie, the OR operator (||
) is applicable. It tests whether at least one of the conditions is true. This equates to checking whether you have either apples or bananas at home to determine which dessert to make:
JavaScript1let hasBananas = false; 2 3if(hasApples || hasBananas){ 4 console.log("We can make a dessert!"); // This will output: "We can make a dessert!" 5}
While the AND (&&
) and OR (||
) operators are great for testing multiple conditions, sometimes you need to check if a condition is not true. This is where the NOT operator (!
) comes into play.
The NOT operator is like saying 'not' or 'no' in English. It inverts the truthiness of a condition. For example, you want to check if the bakery is not closed:
JavaScript1let bakeryOpen = false; 2 3if (!bakeryOpen) { 4 console.log("The bakery is closed, we cannot buy ingredients for desserts."); 5}
Here, if bakeryOpen
is false
, !bakeryOpen
becomes true
, and it will print the message indicating that the bakery is closed.
Congratulations! In this lesson, we have familiarized ourselves with truthy and falsy values, conditional statements, logical operators, and equality operations. We've tied these concepts to a real-life situation: deciding what dessert to make based on the fruits available at home.
In the next steps, you'll apply these concepts yourself in the exercise section. Try creating your own coding scenarios. The more practice you get, the more you'll enhance your skills!