Skip to content

Commit ed0d9df

Browse files
authored
Update js codes.docx
1 parent f86a30f commit ed0d9df

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

‎js codes.docx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,3 +2440,50 @@ var c= new Promise((resolve) => resolve(5));
24402440
var d= Promise.letsBuildAnAny([a,b,c]);
24412441
d.then(result=>console.log(result))
24422442
===========================================================================================================================================================================
2443+
Rest Parameter in Array :
2444+
function addSum(a,b,c, ...rest){ // ...rest indicating combination of those arguments which are left(rest).
2445+
console.log(...rest) //6,7
2446+
console.log(rest) //[6,7]
2447+
console.log(arguments) //ES5 //{"0":2, "1":3, "2":4, "3":6, "4":7}
2448+
return a+b+c+rest[0]+rest[1];
2449+
}
2450+
console.log(addSum(2,3,4,6,7)) //22
2451+
======================================================================================================================================================================
2452+
//Spread Operator in Array:
2453+
function getNames(name1, name2, name3){
2454+
console.log(name1,name2, name3);
2455+
}
2456+
var names =["priya", "riya", "supriya"]
2457+
getNames(names[0], names[1], names[2]); //"priya" "riya" "supriya"
2458+
getNames(...names) //spread operator here used to spread the individual arguments //best approach because here we are passing all the arguments but we can use some of the arguments inside function without an error. Other approaches will gives an error to pass those arguments which are used in function.
2459+
getNames(names)
2460+
=========================================================================================================================================================================
2461+
Rest Spread in object:
2462+
var student ={
2463+
name: "priya",
2464+
age : 100,
2465+
hobbies : ["cooking", "dancing"]
2466+
}
2467+
2468+
//const age = student.age; //earlier we like this
2469+
//console.log(age) //10
2470+
2471+
//const {age, ...rest} = student; //using destructuring
2472+
//console.log(age, rest) //100 {"name": "priya", "hobbies":["cooking", "dancing"]}
2473+
2474+
const {...rest} = student;
2475+
console.log(rest) //{"name": "priya", "age": 100, "hobbies":["cooking", "dancing"]}
2476+
=====================================================================================================================================================================
2477+
//spread operator in object: (Change the value of age)
2478+
var student ={
2479+
name: "priya",
2480+
age : 100,
2481+
hobbies : ["cooking", "dancing"]
2482+
}
2483+
2484+
var newStudent ={
2485+
...student, //coping one object to another object
2486+
age : 101
2487+
}
2488+
console.log(newStudent)
2489+
====================================================================================================================================================================

0 commit comments

Comments
 (0)