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

183
Views
how to pass argument to child function in js
function checkSalary(baseSalary, bonus, tax) {
    console.log(baseSalary, bonus, tax)
    function salary(baseSalary, bonus) {
        console.log(baseSalary, bonus)
        return baseSalary + bonus;
    }
    function salary_with_tax(tax) {
        console.log(tax)

        return salary() - tax;
    }
}

console.log(checkSalary(5000, 3000, 100))


I want to check using salary() and salary_with_tax() but don't know how to pass the arguments.

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

0

You must call your inner functions at some point. Something like this:

I had to make some assumptions about what you want. I returned an object with the results of each inner function.

function checkSalary(baseSalary, bonus, tax) {
    console.log(baseSalary, bonus, tax)
    function salary(baseSalary, bonus) {
        console.log(baseSalary, bonus)
        return baseSalary + bonus;
    }
    function salary_with_tax(tax) {
        console.log(tax)

        return salary(baseSalary, bonus) - tax;
    }
    
    return {
      salary: salary(baseSalary, bonus),
      tax: salary_with_tax(tax)
    }
}

console.log(checkSalary(5000, 3000, 100))

about 4 years ago · Juan Pablo Isaza Report

0

Javascript is much like many languages where you can defined functions within other functions, but until you actually call the function nothing happens. Just call the inner functions and get the result and do with it whatever you like.

There is also no need to re-define the arguments if theyre exactly the same names.

function checkSalary(baseSalary, bonus, tax) {

    function salary() {
        
        return baseSalary + bonus;
    }
    function salary_with_tax() {
        return salary() - tax;
    }
    
    const theSalary = salary()
    const theSalaryWithTax = salary_with_tax();
    
    console.log(theSalary, theSalaryWithTax);
    
    return "Foo"; // you can "check" whatever you like here, and return something more appropriate
}

console.log(checkSalary(5000, 3000, 100))

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!