0

ES6 Destructuring doesnt work inside function()

Hello, i would like to have ES6 destructuring inside a function, but somehow they just dont want to work if i call it with a function parameters instead of their actual variable names. Can anyone tell whats wrong? https://code.sololearn.com/WBPgcEfWpqd5/?ref=app

5th Oct 2022, 5:43 AM
Hizand
Hizand - avatar
9 Answers
+ 3
Bob_Li You need to add a semicolon to the first line with console.log in your snippet. Automatic Semicolon Insertion (ASI) logic see console.log("var1=", var1, "var2=", var2) [var2,var1] = [var1,var2] as console.log("var1=", var1, "var2=", var2)[var2,var1] = [var1,var2] https://blog.alexdevero.com/automatic-semicolon-insertion-in-javascript/
5th Oct 2022, 2:28 PM
ODLNT
ODLNT - avatar
+ 2
On this page, there's a note mentioning how function arguments are passed by value, which means functions work with a copy of the data, and changes to arguments' value does not change actual data represented by the argument. https://www.w3schools.com/js/js_function_parameters.asp Why do it inside a function when it doesn't need to be? Consider passing an array or object as argument. The same page above mentioned it may imitate pass-by-reference behaviour.
5th Oct 2022, 8:00 AM
Ipang
+ 2
It's okay to do destructuring in functions, but looks like we got no choice but to pass array or object in case the data to be exchanged was external to the function : )
5th Oct 2022, 9:49 AM
Ipang
+ 2
ODLNT šŸ˜®šŸ˜® You are right! The destructuring square brackets makes it ambiguous and javascript joins it to the console.log before it, turning it to nonsesnse code... šŸ˜… I'm one of those who get distracted with too many semicolons, and javascript generally plays along fine when I don't use semicolons. I guess this is one of the cases where semicolons matter. Also, the error message was not very helpful in this case. The link to ASI is very helpful. i guess I was being over reliant on it and the destructuring pattern is one of its weak point. Thanks for the nice catch. It was something I would never have thought of.šŸ˜Ž
5th Oct 2022, 3:42 PM
Bob_Li
Bob_Li - avatar
+ 1
Ipang for real?? I have made another code and checked its value (this one is not inside object), and holy shit youre right, the values did not change..... I always do my code inside function so it is easier to use in other project, and easier to implement/disable.
5th Oct 2022, 8:50 AM
Hizand
Hizand - avatar
+ 1
This is weird. console.log before destructuring swap makes it impossible. Anybody have any ideas why? šŸ¤”šŸ¤”šŸ¤” let var1 = 10; let var2 = 5; //console log makes destructure swap impossible. Try uncommenting the console.log below //console.log("var1=", var1, "var2=", var2) [var2,var1] = [var1,var2] console.log("var1=", var1, "var2=", var2)
5th Oct 2022, 12:45 PM
Bob_Li
Bob_Li - avatar
+ 1
The variables have to be re-declared if there is a console log between the declaration and destructuring. var1 = 10 var2 = 5 //console log makes destructure swap impossible. console.log(var1, var2) //var1 and var2 have to be redeclared. Uncomment the two lines below //var1 = 10 //var2 = 5; [var2,var1] = [var1,var2] console.log(var1, var2)
5th Oct 2022, 1:03 PM
Bob_Li
Bob_Li - avatar
0
Java script lesson 25.2 important reminder please tell me about
6th Oct 2022, 7:45 AM
Sarfraz Ahmed Jamali
Sarfraz Ahmed Jamali - avatar
0
Sarfraz Ahmed Jamali idk i dont have that course unlocked
6th Oct 2022, 8:03 AM
Hizand
Hizand - avatar