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

192
Views
How to get the code of the line currently running in JavaScript?

I want to write a function in JavaScript, it needs to get the string form (text in source code) of its parameter instead of the value of the parameter.

Note: The type of the parameter may be anything, not restricted to be of string, and the value of the parameter may be any expression.

For example:

function processParam(param)
{
    let literalCode = ""; 
    ...
}

// I want to get the string "100 * 2" instead of 200 inside this function.
processParam(100 * 2); 


// I want to get the string "console.log('abc')" instead of undefined inside this function.
processParam(console.log('abc')); 

In other words, I want to get the code of the line that is currently running. How can I do this?

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

0

That is not really possible like that. But with a slight change you can get some source code. Functions have source code. So if you pass the argument as a function, you can get the source code. And if you want the actual value, you should execute it.

Here is a demo:

function processParam(param){
    let literalCode = param.toString().split("=>").pop().trim();
    console.log(literalCode);
    let actualValue = param(); // execute it
}

processParam(() =>100 * 2); 
processParam(() => console.log('abc')); 

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!