let a = 1
let b = 2
[a, b] = [b, a]
console.log(a)
console.log(b)
Let b = 2
(without a semicolon) will give an error:
2.
let a = 1
let b = 2
let c = 3
[a, b] = [b, a]
console.log(a)
console.log(b)
console.log(c)
Let c = 3
without a semicolon, no error, c
will turn into an array. Why is this?
Questions stimulate discussion and creative and critical thinking, as well as determine how students are thinking. Questions help students retain material by putting into words otherwise unarticulated thoughts.
When we ask the right questions, we succeed as a thinker, for questions are the force that powers our thinking. Thinking, at any point in time, can go off in thousands of different directions, some of which, by the way, are dead-ends. Questions define the agenda of our thinking. They determine what information we seek.
Probing questions ask for more detail on a particular matter. They're often follow-up questions like, 'Could you tell me more about that?' or 'Please explain what you mean. ' Probing questions are meant to clarify a point or help you understand the root of a problem, so you know how best to move forward.
Automatic semicolon insertion has some quirks, and this is one of them. In your first example, no semicolon will be inserted at the end of line two, and thus it reads to the parser like this:
let b = 2[a, b] = [b, a];
This throws an exception because b doesn't exist yet and yet it's trying to be used to define itself. Example 2 looks similar, but now is referring to variables that already exist, so rather than an exception you just have a really bizarre looking piece of code:
let c = 2[a, b] = [b, a];
As for what this means, well, first let's look at 2[a, b]
. Numbers get boxed to objects, so it is possible to access properties on them. That's why for example, you can do the following:
let temp = 2;
console.log(temp.toString());
temp is a number object, and it has a toString property on it. Now, because the .
character has a special meaning for numbers (it's a decimal point), you can't use it with a number literal, so something like the following is illegal:
2.toString();
But it's fine to use the bracket syntax, as in:
2['toString']()
That bracket syntax is what we have in 2[a, b]
: It's trying to access a property on 2. Which property? 2[b], which resolves to 2[2]. The a
is being used with the weird comma operator, and basically gets ignored.
What's at 2[2]? Nothing really, just a property that's undefined
. And if you try to assign to it, nothing of note is going to happen, because numbers don't let you modify properties on them like that. But you can still try to assign to it if you want, and it will just silently fail (in non-strict mode anyway).
So to summarize what 2[a, b] = [b, a];
does: It creates an array with elements [b, a]
, and then attempts to assign that to 2[2]. This doesn't do anything to the number 2, but the value you tried to assign does become the result of the expression, so the result of 2[a, b] = [b, a];
is just [b, a]
, which is [2, 1]
. This, in turn, gets assigned to c, due to the code let c = //etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With