Skip to content
Advertisements

JavaScript Rest Parameters

When declaring the parameters of a function, the three-point and a parameter declared with a name give all the parameter values ​​as an array, except the parameters that were previously reported to the function. These are called rest (all other parameters) parameters.

function exam(...param) {
            document.write(param.length+"<br>");
        }
        exam(0); 
        exam(1, 2);
        exam(1,2,3,4,5);

output: 1 , 2 , 5              


function multiplication(withWhat, ...numbers) {
            var res = 0;
            for (var i = 0; i < numbers.length; i++) {
                res += numbers[i] * withWhat;}
            return res;
        }
        document.write(multiplication(2,1,2,3,4,5))

output: 30

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: