Skip to main content

Execute function with condition in javascript

In some cases, You have to execute the function with a condition in javascript. Like if you want to perform a task or execute the function on a specific condition then these code snippets will help you to find the solution for you.

  • Get double using arraw function with short-circuits conditional


    condition && do()

    Code snippet

    const getDouble = (val) => { return val + val  }
    let online = true;
    let data = online && getDouble(20);

    Output :


    Short circuiting means that in JavaScript when we are evaluating an AND expression (&&), if the first operand is false, JavaScript will short-circuit and not even look at the second operand

  • Call function getDouble() using if statement in javascript

    const getDouble = (val) => { return val + val  }
    const $condition = true;
      const output = getDouble(20);

    This code snippet will help you to call a function with a condition. Here we have create a function getDouble() to get the double of input data and call the function with if condition. In case if condition is true it will call the function.  

  • Back to code snippet queries related javascript