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

182
Views
How can I access a function by its name present in form of string in JavaScript

Suppose I am inside a function and I have created many functions inside it. From them I want to access a function by its name present in form of a string. How can I do that?

function main(){
  function a(){return "a function called"}
  function b(){return "b function called"}
  function c(){return "c function called"}
  function d(){return "d function called"} // Please assume that these functions do very different stuffs, so that we cannot use ternary ifs (It is just a rough example)
  const randomFunction = prompt("Which function do you want to call?") // Please consider that we use this user input to ask user type something from a, b, c or d. Obviously return value of prompt function would be a string
  // Now I want to access the function by the same name, user entered and I also want to call it.
  // So what can be the best approach to solve this problem, except from using "eval" or "Function"
}
main()
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Turn it into an object and access by key. i.e.

Suppose I am inside a function and I have created many functions inside it. From them I want to access a function by its name present in form of a string. How can I do that?

const randomFunc =  {
  a: function a(){return "a function called"},
  b: function b(){return "b function called"},
  c: function c(){return "c function called"},
  d: function d(){return "d function called"},
}

console.log(randomFunc.a)

you can then access the functions by the user input.

about 4 years ago · Juan Pablo Isaza Report

0

function main(){ function a(){return "a function called"} function b(){return "b function called"} function c(){return "c function called"} function d(){return "d function called"} // Please assume that these functions do very different stuffs, so that we cannot use ternary ifs (It is just a rough example) const randomFunction = prompt("Which function do you want to call?") // Please consider that we use this user input to ask user type something from a, b, c or d. Obviously return value of prompt function would be a string // Now I want to access the function by the same name, user entered and I also want to call it. // So what can be the best approach to solve this problem, except from using "eval" or "Function" return d; } main()
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!