1

Is possible to set default arg for a rest variable in JavaScript? Something like:

function(first,...second=[arg1,arg2]){

}
2
  • 2
    Are you using ES5 or ES6? Commented Mar 12, 2017 at 6:01
  • Thanks for reply , ES6 Commented Mar 12, 2017 at 6:15

1 Answer 1

3

No, that’s not possible, but also not hard to work around:

function (first, ...second) {
    if (second.length === 0) {
        second = [arg1, arg2];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.