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

171
Views
Is it possible to provide default parameters with rest operator

I was learning from an ES6 essential course and trying default parameters and rest operator for functions. I have defined a function sayHi as below with default parameters and then rest operator which does not gives the desired output.

const sayHi = (greetings, ...names) => {

    names.forEach(item => {
        console.log(`${greetings} ${item}`);
    });

}

sayHi('Hi', 'Ahsan', 'Awais', 'Haseeb');

The above snippet works as desired. but when I tried to set a default parameter value for greetings variable it works but gives unwanted result i.e. value 'Ahsan' is taken by the greetings variable.

const sayHi = (greetings = ' Hi', ...names) => {

  names.forEach(item => {
    console.log(`${greetings} ${item}`);
  });

}

sayHi('Ahsan', 'Awais', 'Haseeb');

Is there a way I can set default parameters in function like above before rest operator?

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

0

You can't, no. The rest parameter only picks up the "rest" of the parameters not consumed by any previous formal parameters, so greeting will always receive the first argument's value.

Separately, since both the names and the greeting have the same type, you can't provide a default at all if you want to accept the names that way.

A couple of options for you:

A curried function

You could have a function that accepts the greeting and returns a function that uses it with whatever you pass it:

const greet = (greeting = "Hi") => (...names) => {
    for (const name of names) {
        console.log(`${greeting} ${name}`);
    }
};

greet()("Ahsan", "Awais", "Haseeb");
greet("Hello")("Ahsan", "Awais", "Haseeb");

Note how we called that:

greet()("Ahsan", "Awais", "Haseeb");

greet() creates the function using the default greeting. Then we call that function by using ("Ahsan", "Awais", "Haseeb") on it. greet("Hello") creates a function that uses the greeting "Hello" (and then we call it).

(I also took the liberty of using for..of rather than forEach, but it's a matter of style.)

Take names as an array

Another option is to accept the names as an array. That way, we can tell inside the function whether we got a greeting or not:

const greet = (greeting, names) => {
    if (Array.isArray(greeting)) {
        names = greeting;
        greeting = "Hi";
    }
    for (const name of names) {
        console.log(`${greeting} ${name}`);
    }
};

greet(["Ahsan", "Awais", "Haseeb"]);
greet("Hello", ["Ahsan", "Awais", "Haseeb"]);

about 4 years ago · Juan Pablo Isaza Report

0

you just have to pass undefined in the 1st parameter to skip optional arg
think of it like this greetings = typeof greetings != 'undefined' ? greetings : "hi" which means that check the value of greetings and if it's undefined (not provided) use the default value
edit: here is the code snippet

const sayHi = (greetings = ' Hi', ...names) => {

  names.forEach(item => {
    console.log(`${greetings} ${item}`);
  });

}

sayHi(undefined, 'Ahsan', 'Awais', 'Haseeb');
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!