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?