Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask everyone a question, how is this analyzed?

Tags:

javascript

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:

enter image description here

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?

enter image description here

like image 668
crane Avatar asked Feb 21 '19 03:02

crane


People also ask

How does asking questions help you learn?

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.

Why is asking questions important in critical thinking?

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.

What is the purpose of asking probing questions?

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.


1 Answers

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

like image 188
Nicholas Tower Avatar answered Oct 02 '22 14:10

Nicholas Tower