javascript
Use of logical operators to reduce if else statements Javascript
We can use logical operators in javascript to reduce the if else statements. As we can use logical operators as an option sometimes.
function run_func(number) {
// set number to 35 if it is not already set
number = number || 35;
console.log("Number is : " + number);
}
const check_this_var = 20;
check_this_var === 20 && run_func();
// SAME AS - if (check_this_var === 20) { run_func(); }
// Output: 10
check_this_var === 5 || run_func();
// SAME AS if (check_this_var != 5) { run_func(); }
// Output: 10
Example Demo
Was this helpful?
Similar Posts
- How to use Ternary Operator in place of if else statements Javascript
- Lodash - Check if a number is within the Range(without if-else)
- Use of ?? (Nullish Coalescing Operator) in Javascript
- How to use loop in React JSX
- Use image as chart datasets background Chart.js
- Use jQuery to check if a tab is active/inactive
- Convert all characters to lowercase using toLowerCase() method in javascript