-2

Getting wrong results when destructuring array into independent variables

I tried to switch the values of two variables using this notation "[Agered,Kr]=[Kr,Agered]" and it didnt work,they didnt change. Later in the code, I am trying to destructure an array and pass its first three elements into independent number variables, using the Array destructuring notation "let [a,b,c]=[Arrayfor]", but in place of 'a' and 'b', I get values of the variables "Agered" and "Kr" which I initialized earlier. The variable 'c' only returns 'undefined'. None of them returns the values in those respective indexes from the array "Arrayfor"... Why does this happen when I try to switch "Agered" and "Kr" using the Array notation, but the program works perfectly when i change it to switching them using the object notation which is commented out in the code?

The image is attached just below, of the results of the array destructuring when using Object Notation to switch the variables "Agered" and "Kr", and the program does return the three variables as the first three elements of the array.Here is the program running smoothly, returning the three elements as expected

The second image of the other situation is also attached below here, where the variables storing the elements from the array return values of eariler defined variables for the first two and the third reads undefined. All this after i attempt to switch the eariler variables "Agered" and "Kr" using Array notation like shown in the code and mentioned earlierHere is the unexpected results

//Arrays instantiated
var Agered=34; 
var Kr=90;

let Arrayfor=[45,78,23,67,[43,78,42,22,65]]

//switching variable values using object notation. And this works

//var {Agered:Kr, Kr:Agered}={Agered,Kr}
 
//Trying to switch the values using array notation. This Fails and affects my destructuring of the array, "Arrayfor", returning values of the earlier variables "Agered" and "Kr" instead of values from the array

[Agered,Kr]=[Kr,Agered]

console.log(Agered,Kr)

//destructuring an array into independent variables
let [a,b,c] = Arrayfor

//The results of the first two variables are the values of "Agered" and "Kr", the last variable returns //undefined like in the image
console.log(a,b,c)

I was expecting to get the first three elements of the array but it didnt work until i changed the switching notation of the values for the earlier variables into object notation. I want to know why the first notation causes unexpected results for a completely independent operation from itself

  • 2
    try `let Arrayfor=[45,78,23,67,[43,78,42,22,65]];` ... <=== you missed a semicolon – Jaromanda X Aug 15 '23 at 08:09
  • Thanks man..You are a genuis. It worked. I just had another bug that was stopping it from working. But it did. Thanks.....I usually don't add them and things work just fine, could you pleas elaborate why I needed the semicolon here specifically and not in the rest of the code? – ManuelPrhyme Aug 15 '23 at 08:17
  • the semicolon in javascript is a complex thing - there's a question here on SO that covers it in great detail (which has been linked) - note: I'm in the camp of ALWAYS using `;` where it should be - makes life so much less random :p – Jaromanda X Aug 15 '23 at 08:18
  • Please resend the link. I cant seem to find it – ManuelPrhyme Aug 15 '23 at 08:23
  • look at your question :p it's given in the close reason – Jaromanda X Aug 15 '23 at 08:25

0 Answers0