Use Destructuring Assignment to Assign Variables from Arrays - Help Understanding Syntax

Hey everyone!

I understand that to solve this challenge you simply have to put return [a , b] = [b, a] in the function body, but I have a couple of questions about the syntax here. I guess I am still trying to wrap my head around some of the arrow function syntax in this ES6 section.

  1. How does the function bracketing syntax below work? I get that since it is an anonymous function, we don’t have to give it a name, but why do we not need to pass in the arguments a and b? Is it just because a and b were declared with ‘let’, so they are already accessible inside the function?

(() => {
// code here

  1. What is the purpose of the (); at the end of the closing brackets?

Thanks everyone for your help!

Your code so far

let a = 8, b = 6;
(() => { // Why no arguments passed into ()? // Why no function keyword??
  "use strict";
  // change code below this line
  return [a, b] = [b, a] 
  // change code above this line
})(); //What is the purpose of the (); after the function body? 
console.log(a); // should be 6
console.log(b); // should be 8

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:

  1. Your assumption is correct. a and b are declared with let keywords but they can be accessed by the inner scopes.

  2. That is so that the function can be executed immediately. I will refer you to IIFE. Basically if you use IIFE, you don’t necessarily need to give the function a name and call it on a separate line.

1 Like