17
loading...
This website collects cookies to deliver better user experience
if/else if
and switch
statements. Why? Because in JS, we can do better than just using if
.if
, and let’s refactor it using the ternary operator.if (condition) {
return functionTrue();
} else {
return functionFalse();
}
return
. The ternary operator allows for simplification:return condition ? functionTrue() : functionFalse();
?
), an expression for a truthy conditional followed by a colon (:
), and finally an expression for a falsy conditional. Here is what it looks like:condition ? expressionIfTrue : expressionIfFalse
true
and false
expressions must be provided for the ternary operator to work. But what if we only need to do something when the condition is truthy?&&
and ||
.if (condition) {
console.log("it's true!");
}
&&
, like so:condition && console.log("it's true!");
&&
, if the first statement is falsy, there’s no point in evaluating the next, as the whole expression is falsy.||
operator will continue evaluating the operands until one of them is true
, or the whole expression evaluates to false
. Take a look at the below example:trueCondition || console.log("Hello world!"); // does not execute the console.log
falseCondition || console.log("Hello world!"); // executes the console.log
if
conditions — such as in the following function, which takes the name of a fruit and returns its color.function getColor(fruit) {
if (fruit.toLowerCase() === 'apple') {
return 'red';
} else if (fruit.toLowerCase() === 'banana') {
return 'yellow';
} if (fruit.toLowerCase() === 'orange') {
return 'orange';
} if (fruit.toLowerCase() === 'blueberry') {
return 'blue';
} if (fruit.toLowerCase() === 'lime') {
return 'green';
}
return 'unknown';
}
toLowerCase
is being called multiple times for each fruit, which could not only affect performance but also make the whole function less readable.toLowerCase
method in one of our lines.switch
statement.function getColor(fruit) {
switch(fruit.toLowerCase()) {
case 'apple':
return 'red';
case 'banana':
return 'yellow';
case 'orange':
return 'orange';
case 'blueberry':
return 'blue';
case 'lime':
return 'green';
default:
return 'unknown';
}
}
function getColor(fruit) {
const fruits = {
'apple': 'red',
'banana': 'yellow',
'orange': 'orange',
'blueberry': 'blue',
'lime': 'green',
};
return fruits[fruit.toLowerCase()] || 'unknown';
}
if
statements is called Jump Table. It can work for much more than simple texts or constants; let’s see a more complex example.if
statements have multiple lines of codes with function calls?calculate
function with two numbers and an operation as an argument, and return the operation’s result over the two numbers.function calculate(number1, number2, operation) {
const operations = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b,
}
return operations[operation]?.(number1, number2) ?? 'invalid operation';
}
return
statement; but the idea behind it is simple, so let’s break it down.operations[operation]?.(number1, number2)
undefined
. This last part is thanks to the optional chaining operator.null
or undefined
and otherwise returns its left-hand side operand.?? 'invalid operation';
invalid operation
.