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

199
Views
JS pass default parameter as a variable that is defined inside that function?

For example:

function mf(z, x c, lol = b) { // I need lol = azazaz
  let b = azazaz
  ...
}

Instead of lol = azazaz I obviously get b is not defined.

What I can do:

function mf(z, x, c, lol = "b") { //parameter lol is a string
  let b = azazaz
  lol = eval(lol) //now lol = azazaz
  ...
}

Also I can do this:

function mf(z, x, c, lol) { //parameter lol doesn't have default value
  b = azazaz
  if (lol == 0) {
    lol = b             //default value of lol = azazaz
  }
  ...
}

But the first one looks so non-professional, the second one has extra if statement which I also don't want. Is there any better way to do this?

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

0

If defining the variable inside of the function isn't a hard requirement, you can probably leverage closures to do something like:

const b = 'azazaz';
function mf(z, x, c, lol = b) { 
 ...
}

Or, perhaps use 'OR' to avoid if

function mf(z, x, c, lol) { //parameter lol doesn't have default value
  let b = 'azazaz';
  lol = lol || b;
  ...
}
about 4 years ago · Juan Pablo Isaza Report

0

If you need to change paramater and re-use it you must re-call the function like:

//every time function change lol and recall function
function mf(z, x, c, lol) {
  console.log('lol now is: ' + lol);
  lol++;
  if (lol <= 10) {
    mf(1, 2, 3, lol);
  }
}

mf(1, 2, 3, 0);

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!