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

263
Views
How to execute a function in JavaScript/typescript whose definition is stored in string

How can i call a function whose definition is stored in string? e.g

var dynamicFun = `function Hello(person) {   return 'Hello' + person; }`

here the dynamicFun variable can hold any function definition dynamically e.g it can be

var dynamicFun = `function Sum(num) {   return num + num; }`

At compile time I would be not aware which function definition is assigned to dynamicFun variable. It is getting assigned at runtime so How can I call the above functions using dynamicFun variable with params (without knowing the actual function name or definition) in javascript/typescript.

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

0

I would take a look at the Eval Global Function in JS.

By the way, this isn't considered great practice for security reasons. I'm curious as to why you might be looking to do this as there might be a better solution.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

console.log(eval('2 + 2'));
console.log(eval('"Hello" + " " + "World"'));

I hope this answers your question!

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!