The Rest parameter is a feature in JavaScript that 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 parameters.
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(1, 2, 3, 4, 5)); // Output: 15
In this example, the sum
function takes any number of arguments and uses the rest parameter ...numbers
to collect them into an array. The function then iterates over the array and adds up all the values before returning the result.
the Rest parameter ko hum ek feature kehte hain jo JavaScript mein hota hai aur jiski madad se ham indefinite number of arguments ko ek array ke roop mein represent kar sakte hain. Yah teen dots (…) ke dwara represent kiya jaata hai aur phir uss array ka naam diya jaata hai jismein baaki ke parameters store ho jaayenge.
Rest parameter ke dwara values assign kaise karte hain? Example ke saath detail mein samjhaaiye.
Rest parameter JavaScript mein ek feature hai jo ki humein allow karta hai multiple arguments ko ek variable mein store karne ke liye. Isse hum functions ko flexible bana sakte hain, jisse ki humein aane wale future changes ke liye tayaar rehna hota hai.
Iska use karne ke liye, function ke last argument ke pahle three dots (...) lagaate hain, jise rest parameter kahte hain. Yeh sabhi arguments ko ek array mein store karega.
Let's take 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(1, 2, 3, 4, 5)); // Output: 15
Iss code mein, sum()
function ke andar rest parameter ...numbers
ka use kiya gaya hai. Isse hum chahe jitne bhi arguments pass kar sake hain, aur unhe numbers
naam ke ek array mein store kara skate hain. Fir hum for
loop se unke sum ko calculate kar rahe hain.
Iss code se aap yeh dekh sakte hain ki humne rest parameter ka use karke function ko flexible banaya hai, jisse future mein arguments add karna bahut hi aasan ho jaayega.
Rest element is a feature of JavaScript that allows you to represent an indefinite number of arguments as an array. The syntax for the rest element is three dots followed by a variable name, like so: ...variableName
.
The rest element must be the last parameter in a function's parameter list, and it collects all remaining arguments passed to the function into an array assigned to the variable.
Let me give you an example in Hinglish:
function printNames(first, second, ...names) {
console.log("First name: " + first);
console.log("Second name: " + second);
console.log("Remaining names: " + names);
}
printNames("Amitabh", "Abhishek", "Aishwarya", "Jaya", "Shweta");
// Output:
// First name: Amitabh
// Second name: Abhishek
// Remaining names: Aishwarya,Jaya,Shweta
In this example, we have defined a function called printNames
which takes in two mandatory parameters (first
and second
) followed by the rest element ...names
. When we call the function with five arguments, the first two are assigned to first
and second
variables respectively, and the rest of the arguments ("Aishwarya"
, "Jaya"
, and "Shweta"
) are collected into an array assigned to names
.
Finally, the function prints out the values of all three variables using console.log()
. As you can see, the output shows the values assigned to first
and second
along with the array containing the remaining arguments.
Rest operator is a JavaScript feature that allows you to assign multiple values to an object or an array. Using the rest operator, you can easily assign multiple values to an object in a single line of code.
Here's an example of how you can use the rest operator to assign values to an object:
// Example Object
const student = {
name: 'John Doe',
age: 25,
address: '123 Main Street',
phone: '555-555-5555'
};
// Using Rest Operator to set new key-value pairs in the same object
const updatedStudent = {...student,
age: 26,
email: 'johndoe@example.com'
};
console.log(updatedStudent) // { name: 'John Doe', age: 26, address: '123 Main Street', phone: '555-555-5555', email: 'johndoe@example.com' }
In this example, we have an object student
with four key-value pairs. We want to update the age
and add a new key-value pair for email
.
Using the rest operator (...student
), we are creating a copy of the student
object and adding any additional key-value pairs necessary. The updatedStudent
object now has five key-value pairs, including the updated age
and the new email
.
Rest operator ka upyog karke aap ek hi line mein object ke kai key-value pairs ko set kar sakte hain. Iska upyog karke, aap asaani se ek object mein kai value ko assign kar sakte hain. Upar diye gaye udaharan mein hamne ek student naam ka object banaya hai jismein char key-value pairs hain. Ham usmein age ko update karna chahte hain aur ek naya key-value pair email ke liye bhi add karna chahte hain.
Rest operator ka upyog karke (...student
), hum student object ka copy banakar kisi extra key-value pairs ko add kar rahe hain. updatedStudent
object mein ab panch key-value pairs hain, jismein updated age aur naya email shamil hain.
Rest parameter allows you to pass an indefinite number of arguments as an array to a function. This feature is denoted by "..." before the last parameter name in a function declaration. Here's an example:
function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
In the above example, the ...numbers
parameter is the rest parameter that allows us to pass any number of arguments to the sum
function. The passed arguments are collected into an array called numbers
. Then, we use a loop to iterate over the array and add up all the values to get the total sum.
Rest parameters are useful when you need to work with functions that can take a variable number of arguments. It eliminates the need to specify exactly how many arguments a function should expect, making your code more flexible and concise.
Rest parameter ka use karke aap ek function mein anant sankhya mein arguments ko array ke roop mein pass kar sakte hain. Is feature ko ...
last parameter ke naam se declare kiya jata hai. Yahan ek udaharan hai:
function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
Upar diye hue udaharan mein ...numbers
parameter rest parameter hai, jo humein sum
function mein kisi bhi sankhya ke arguments ko pass karne ki anumati deta hai. Ye passed arguments numbers
naam ke array mein sanchit kiye jaate hain. Phir, hum ek loop ka upyog karke array par aavarti hote hain aur sabhi maan ko jodkar kul yog prapt karte hain.
Rest parameters ka upyog tab kiya jaata hai jab aap functions ke saath kaam kar rahe hote hain jismein variable number of arguments liye jaate hain. Isse function mein kitne arguments hona chahiye yah specify karne ki zaroorat nahi hoti hai, jisse aapka code flexible aur laghu ho jata hai.