Skip to content

Latest commit

 

History

History
116 lines (77 loc) · 5.12 KB

34-Rest-parameter.md

File metadata and controls

116 lines (77 loc) · 5.12 KB

What is The Rest Parameter ?

The rest parameter in JavaScript allows a function to accept an indefinite number of arguments as an array. It is represented by three dots (…) followed by the parameter name.

Here's an example:

function sum(...numbers) {
  let total = 0;
  for (const num of numbers) {
    total += num;
  }
  return total;
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(4, 5, 6, 7)); // Output: 22

In the above code, the sum function accepts an indefinite number of arguments using the rest parameter syntax ...numbers. The function then iterates over the numbers array and adds them up to return the total sum. We can call the sum function with any number of arguments, and it will work correctly.

How to Assign values using rest parameter ?

Rest parameters in JavaScript allow you to represent an indefinite number of arguments as an array. You can then use this array to assign values to variables. Here's an example:

function sum(...numbers) {
  let total = 0;
  for (const num of numbers) {
    total += num;
  }
  return total;
}

const result = sum(1, 2, 3, 4, 5);
console.log(result); // Output: 15

In the above example, we have defined a function sum that takes any number of arguments using rest parameter syntax (...numbers). It then iterates through the array of numbers using a for...of loop and calculates the sum of all the numbers. Finally, it returns the total.

We can call the sum function with any number of arguments, and rest parameters will convert them into an array. In this case, we passed five arguments - 1, 2, 3, 4, and 5 - which were assigned to the numbers array inside the sum function.

We then assigned the return value of the sum function to a variable called result, which we logged to the console. The output is 15, which is the sum of all the numbers passed to the sum function.

What is Rest element last element concept?

The rest element in JavaScript allows you to represent an indefinite number of arguments as an array. It is denoted by three dots (...) followed by the name of the array that will contain the rest of the elements.

Here's an example:

function sum(...numbers) {
  let result = 0;
  for (let i = 0; i < numbers.length; i++) {
    result += numbers[i];
  }
  return result;
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(4, 5, 6, 7, 8)); // Output: 30

In this example, the sum function takes an indefinite number of arguments and uses the rest parameter ...numbers to capture them into an array. The function then loops through the array and adds up all the numbers, returning the sum.

Note that the rest parameter must be the last parameter in the function definition, as it collects all remaining arguments passed to the function.

How to Assign values using rest operator in object ?

In JavaScript, the rest operator can be used to assign values to an object. The rest operator allows you to gather all remaining properties and pack them into an array. Here's an example:

const { name, age, ...rest } = { name: "John", age: 30, city: "New York", country: "USA" };

In the above example, we have an object with four properties: name, age, city and country. We are using object destructuring to assign the values of name and age to variables of the same name, while the remaining properties are assigned to a variable called rest using the rest operator.

After executing this code, the value of name will be "John" and age will be 30, while the rest variable will contain an object with the properties city and country.

You can also use the spread operator to merge two or more objects, as shown below:

const obj1 = { name: "John", age: 30 };
const obj2 = { city: "New York", country: "USA" };

const newObj = { ...obj1, ...obj2 };

In the above example, we have two objects obj1 and obj2, which have different properties. We are merging these two objects into a new object called newObj, using the spread operator. The resulting object has all the properties from both obj1 and obj2.

How to pass Variable arguments in function using rest parameter ?

Rest parameter in JavaScript allows a function to accept an indefinite number of arguments as an array. To use rest parameter, you need to prefix the last named parameter of a function with an ellipsis (...).

Here is an example of using rest parameter in a function:

function sum(...numbers) {
  let result = 0;
  for (let i = 0; i < numbers.length; i++) {
    result += numbers[i];
  }
  return result;
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(4, 5, 6, 7, 8)); // Output: 30

In the above example, the sum function accepts any number of arguments and stores them in an array numbers using the rest parameter syntax (...numbers). The for loop iterates through each element of the numbers array and adds them up to get the final result.

Note that the rest parameter must be the last parameter in the function's parameter list.