How a print as output 2 and b 6, c 8 and d 4? Would please help me to solve it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How a print as output 2 and b 6, c 8 and d 4? Would please help me to solve it?

let a, b, c = 4, d = 8; [a, b = 6] = [2]; console.log(a); // 2 console.log(b); // 6 [c, d] = [d, c]; console.log(c); // 8 console.log(d); // 4

15th Jan 2023, 8:28 AM
Sony
Sony - avatar
3 Answers
+ 2
Read the reference about destructuring assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment let a, b, c = 4, d = 8; [ a, b = 6 ] = [ 2 ]; <a> gets it value from right hand side array [ 2 ] <b> gets its value from default value that was specified in left hand side array. * Read the "Default value" section in the above link [ c, d ] = [ d, c ]; Value of <c> and <d> are swapped. * Read the "Swapping variables" section in the above link
15th Jan 2023, 9:10 AM
Ipang
+ 2
Ipang thanks .
15th Jan 2023, 9:51 AM
Sony
Sony - avatar
0
No problem, it's a good query 👍
15th Jan 2023, 10:10 AM
Ipang