Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Passing on function with switch dynamically

I am new to javascript and I am trying to figure out how to dynamically pass on functions with switch?

For example, I can pass on function manually like following

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

var text = `the value is ${add(value)}`

console.log(text);

How can I pass on a function with a switch based on the select value? The following does not work.

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

//switch statement
switch(select){ case 1: add(value); break; case 2: subtract(value); break; case 3: multiply(value); break};

var string = `this value is ${switch(select){ case 1: add(value); break; case 2: subtract(value); break; case 3: multiply(value); break};}`

console.log(string)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I suggest moving the switch bloc inside a function, like the following:

var select = 1;
function add(val) { return val + 512 };
function subtract(val) { return val - 300 };
function multiply(val) { return val * 584 };
var value = 290;



var string = `this string in ${chooseFunction(select, value)}`

console.log(string)

function chooseFunction (select, value) {
  // switch statement

  // Note that the break statement is optional 
  // if you return a value at the end of a case block
  switch (select) {
    case 1: return add(value); 
    case 2: return subtract(value); 
    case 3: return multiply(value); 
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

You write a function that has the switch returning the result of calling functions based on the input, and use that in the template string.

function add(val) {
  return val + 512;
}

function subtract(val) {
  return val - 300;
}

function multiply(val) {
  return val * 584;
}

function run(value, select) {
  switch (select) {
    case 1: return add(value);
    case 2: return subtract(value);
    case 3: return multiply(value);
  }
}

const value = 290;

const string1 = `This string is ${run(value, 1)}`;
const string2 = `This string is ${run(value, 2)}`;
const string3 = `This string is ${run(value, 3)}`;

console.log(string1);
console.log(string2);
console.log(string3);

about 4 years ago · Juan Pablo Isaza Report

0

You cannot pass a switch directly, as you cannot pass a if statement without a ternary operator. You need to encapsulate your switch statement in a function like this :

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

const getValue = (s, v) => {
switch(s) {
  case 1:
    return add(v);
  case 2:
    return subtract(v);
  case 3:
    return multiply(v);
   };
}

var string = `this value is ${getValue(select, value)}`

console.log(string)

An other way to do this is to put your functions into an array with the index referencing the choice :

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

const fn = [add, subtract, multiply];

var string = `this value is ${fn[select - 1](value)}`

console.log(string)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!