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

84
Views
An argument with default value precedes non-default argument in JavaScript

In JavaScript it's possible to define a function like this:

function func(a = 10, b)
{
  return a + b;
}

How does one call this function by only specifying the value for b?

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

0

It'd only be possible by explicitly passing undefined, and by also providing a default for the second argument.

function func(a = 10, b) {
  return a + b;
}

console.log(func(undefined, 5));

But this is quite weird - it's very confusing. I'd highly recommend not doing this at all - either put the arguments with default values at the end, or in an object.

function func({ a = 10, b }) {
  return a + b;
}

console.log(func({ b: 5 }));

about 4 years ago · Juan Pablo Isaza Report

0

If you are okay with currying the first parameter:

bind saves the function with the arguments supplied, so when one calls it, it calls it with the arguments applied. The first argument is what to bind this to, so the second argument will bind to a:

function func(a, b) { 
    return a + b;
}

newFunc = func.bind(null, 10)
newFunc(2)
12
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!