Skip to main content

Conditional function calls with parameters


This code sample is easy enough to read that you can tell that you send an operation AND a value and the code knows what to do with both of them. Pay attention to the return keywords here.

function double(x) {
return x * 2
}

function run(operation, x) {
if (operation === "double") {
return double(x) // take the x parameter and pass it to double
}
}

// Sample usage
run("double", 4) // will return 8
run("double", 5) // will return 10