Conditional statements are an important part of JavaScript. It can execute code based on whether a certain condition is true or false, and multiple can be nested. other statements (and other than that) to evaluate multiple conditions.
But here comes the problem. if…otherwise Chaining can quickly make things messy and result in code that is hard to read and understand.
Learn how to do long and complicated refactorings if…else if…else It makes the conditional chain a more concise, cleaner and easier to understand version.
Complex if…else chains
Writing clean, concise, and understandable code is essential when writing complex if…else statements in JavaScript. for example, if…otherwise A conditional chain in the function below:
function canDrink(person) {
if(person?.age != null) {
if(person.age < 18) {
console.log("Still too young")
} else if(person.age < 21) {
console.log("Not in the US")
} else {
console.log("Allowed to drink")
}
} else {
console.log("You're not a person")
}
}const person = {
age: 22
}
canDrink(person)
The logic here is simple.first time If The statement is Man the object is Year property (otherwise he or she is not a person).among them If block, added if… if not… if A chain that basically says:
If the person is under the age of 18, they are too young to drink. If you are under the age of 21, you are under the legal drinking age in the United States. Otherwise, they can legally get their drinks.
The code above is valid, but the nesting makes it difficult to understand. Luckily, you can refactor your code to make it cleaner and easier to read. guard clause.
guard clause
anytime If In the statement that wraps all your code, guard clause To remove all nesting:
function canDrinkBetter() {
if(person?.age == null) return console.log("You're not a person") if(person.age < 18) {
console.log("Still too young")
} else if(person.age < 21) {
console.log("Not in the US")
} else {
console.log("Allowed to drink")
}
}
At the start of the function, we defined a guard clause to indicate that the function should exit if certain conditions are not met. canDrinkBetter() It works immediately (logs “you are not a person” to the console).
However, if the condition is met, if…otherwise Chain to see applicable blocks. Running the code produces the same result as the first example, but this code is easier to read.
don’t use single return
You might argue that the above technique is not a good programming principle because you are using multiple returns in the same function. Also, you might think it’s better to use only one return statement (aka single return policy).
However, this is a terrible way to write code because you end up in the same crazy nested situation you saw in the first code sample.
i.e. you can use multiple return Statements to further simplify the code (and remove nesting):
function canDrinkBetter() {
if(person?.age == null) return console.log("You're not a person") if(person.age < 18) {
console.log("Still too young")
return
}
if(person.age < 21) {
console.log("Not in the US")
return
}
console.log("Allowed to drink")
}
This code works the same as the previous two examples, just a little cleaner.
The last code block was cleaner than the first two, but still not the best.
instead of lengthening if…otherwise Chaining within one function allows you to create another function canDrinkResult() It will do the check for you and return the result:
function canDrinkResult(age) {
if(age < 18) return "Still too young"
if(age < 21) return "Not in the US"
return "Allowed to drink"
}
Then, inside the main function, first apply the guard clause, then canDrinkResult() Use a function (with age as a parameter) to get the result.
function canDrinkBetter() {
if(person?.age == null) return console.log("You're not a person") let result = canDrinkResult(person.age)
console.log(result)
}
In this case, I delegated the task of checking drinking age to a separate function and called it only when necessary. This makes the code cleaner and easier to work with than all the previous examples.
Keep else out of conditionals
You learned how to use guard clauses and function extraction techniques to refactor complex, nested conditional chains into shorter, more readable ones.
please try to keep other than that Use both guard clauses and function extraction techniques to keep statements as far away from conditionals as possible.
New to using JavaScript? if…otherwise Statement, start with the basics.