function addNumber (num1, num2){ consle.log(arggrement[3]); const result = num1 num2; return result; } const sum = addNumber(23,13 50, ); console.log(sum) 1

function addNumber (num1, num2){ consle.log(arggrement[3]); const result = num1 num2; return result; } const sum = addNumber(23,13 50, ); console.log(sum)

function addNumber (num1, num2){
    consle.log(arggrement[3]);
    const result = num1 + num2;

    return result;
}
const sum = addNumber(23,13 50, );
console.log(sum)

Here is what the above code is Doing:
1. We are creating a function called addNumber.
2. We are passing two parameters, num1 and num2.
3. We are creating a variable called result and assigning it the value of num1 + num2.
4. We are returning the value of result.
5. We are creating a variable called sum and assigning it the value of the function addNumber.
6. We are passing the arguments 23 and 13 to the function addNumber.
7. We are logging the value of sum to the console.

The output of the above code will be 36.

Now, let’s look at the code again:

const sum = addNumber(23,13 50, );

We are passing three arguments to the function addNumber, but we only have two parameters.

What happens to the third argument?

The third argument is stored in an array called arguments.

We can access the third argument by using arguments[2].

Let’s log the third argument to the console:

function addNumber (num1, num2){
consle.log(arggrement[3]);
const result = num1 + num2;

return result;
}
const sum = addNumber(23,13 50, );
console.log(sum)

The output of the above code will be 50.

We can also access the third argument by using the rest operator.

The rest operator is three dots (…) followed by the name of the parameter.

Let’s use the rest operator to access the third argument:

function addNumber (num1, num2, …rest){
consle.log(rest);
const result = num1 + num2;

return result;
}
const sum = addNumber(23,13 50, );
console.log(sum)

The output of the above code will be 50.

The rest operator is useful when we don’t know how many arguments will be passed to the function.

Let’s look at another example:

function addNumber (num1, num2, …rest){
const result = num1 + num2;

for (let i = 0; i < rest.length; i++){ result += rest[i]; } return result; } const sum = addNumber(23,13 50, 60, 70, 80); console.log(sum) Here's what the above code is doing: 1. We are creating a function called addNumber. 2. We are passing two parameters, num1 and num2. 3. We are creating a variable called result and assigning it the value of num1 + num2. 4. We are using a for loop to loop through the array rest. 5. We are adding the value of each element in the array rest to the variable result. 6. We are returning the value of result. 7. We are creating a variable called sum and assigning it the value of the function addNumber. 8. We are passing the arguments 23, 13, 50, 60, 70, and 80 to the function addNumber. 9. We are logging the value of sum to the console. The output of the above code will be 286. The rest operator is useful when we don't know how many arguments will be passed to the function. Let's look at another example: function addNumber (num1, num2, ...rest){ const result = num1 + num2; for (let i = 0; i < rest.length; i++){ result += rest[i]; } return result

Similar Posts