In everyday life, we achieve something because we have made some decisions. Some decision we make can lead us to good results and other decisions can destroy our life forever. One of the current decisions I have made is to become a software Developer and now I am learning my first programming language, Javascript.
In Javascript, like in any programming language, the code makes decisions depending on different inputs.
In this blog, I am going to discuss, the decisions we make depending on conditions.
Decision making will depend on the current situation or conditions for example when it is cold you turn on the heat, when feel hungry you cook or you make order at the restaurant.
In Javascript conditional statements allow us to represent decision making from the choice that must be made, to the resulting outcome of those choices.
The most common type of conditional statement in Javascript is IF...ELSE statement. The structure looks like this:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
With the if..else statement, sometimes we have two scenarios, for example if you are hungry, you cook, if you are not you watch TV. It is possible to have a lot of scenario.
Let take the following example.
let weather = "cloudy";
if (weather === "raining") {
console.log("Don't forget your umbrella!");
} else if (weather === "sunny") {
console.log("Enjoy the sunshine!");
} else {
console.log("Well, at least it's not raining!");
}
The if...else statements can go from simple scenarios to very complex scenarios. we can have a situation in which a condition is depending on another condition; in some case we need to use a nested if...else block. Let us use this example:
if (choice === "sunny") {
if (temperature < 86) {
console.log( `It is ${temperature} degrees outside — nice and sunny. Let's go out to the beach, or the park, and get an ice cream.`);
} else if (temperature >= 86) {
console.log(`It is ${temperature} degrees outside — REALLY HOT! If you want to go outside, make sure to put some sunscreen on.`);
}
}
If you want to test multiple conditions without writing nested if...else statements, logical operators can help you.
&& — AND; allows you to chain together two or more expressions so that all of them have to individually evaluate to true for the whole expression to return true.
|| — OR; allows you to chain together two or more expressions so that one or more of them have to individually evaluate to true for the whole expression to return true.
To give you an AND example, the previous example snippet can be rewritten to this:
if (choice === "sunny" && temperature < 86) {
console.log(`It is ${temperature} degrees outside — nice and sunny. Let's go out to the beach, or the park, and get an ice cream.`);
} else if (choice === "sunny" && temperature >= 86) {
console.log(`It is ${temperature} degrees outside — REALLY HOT! If you want to go outside, make sure to put some sunscreen on.`);
}
Let's look at a quick OR example:
if (iceCreamVanOutside || houseStatus === "on fire") {
console.log("You should leave the house quickly.");
} else {
console.log("Probably should just stay in then.");
}
The if...else statement is very important in web development, for example we can use it in password validation when building a web application. we can make a function with if..else conditions that check the strength of the password whether it is easy, medium, difficult or extremely difficult to guess. This is an example of a function that checks if a user's password is strong enough.
function checkPasswordStrength(password) {
if (password.length < 8) {
console.log("Password is too short!");
} else if (!/\d/.test(password)) {
console.log("Password should contain at least one number!");
} else if (!/[A-Z]/.test(password)) {
console.log("Password should contain at least one uppercase letter!");
} else {
console.log("Your password is strong!");
}
}
checkPasswordStrength("WeakPwd1"); // Output: Password should contain at least one uppercase letter!
In conclusion, If...else is very important in Javascript. It is used mostly in all web applications. Every web development student should have a strong understanding of this condition statement as the foundation of decision making scenarios.
Sources:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals.
https://marketsplash.com/tutorials/javascript/javascript-if-else/
Top comments (0)