This question already has answers here:
How does [b][b = a,0] swap between a and b?
(2 answers)
Closed 9 years ago.
I do not know how to learn demo 2,because it's difficult for me.
//demo1.js
var a = 1;
var b = 2;
var c;
c = b;
b = a;
a = c;
log(a); // a = 2
log(b); // b = 1 I can read this one.
//demo 2.js
var a = 1, b = 2;
a = [b][b = a, 0]; // why? '0' is a varible?
console.log(a,b) //output a = 2, b =1
// v---- 1. v---- 3.
a = [b][b = a, 0];
// ^---^-- 2.
Put the value of the b variable in a new Array.
The next set of square brackets is the member operator used for getting the index. In this case, that space is being used to reassign the value of the a variable to the b variable. This can be done because the original value of b is safely in the Array (from step 1.).
Separated by comma operator, the 0 index is then used as the actual value of the Array being retrieved, which if you'll recall is the original b value. This is then assigned to a via the first = assignment on the line.
So to summarize, b is put in the Array, and is retrieved via the 0 index and assigned to a on the left, but not before a is assigned to b (borrowing the space of the [] member operator).
This could also be written like this:
a = [b, b = a][0];
Only difference now is that the second index of the Array is used to do the assignment. Probably a little clearer like this.
The comma operator in Javascript evaluates its left operand, throws it away and returns its right operand after evaluating. Its only use is when the left operand has a side-effect (like modifying a variable's value) but you don't want its result.
[b] defines an array containing b.
[b = a, 0] evaluates b = a - so b now contains the value of a. Then it throws it away. Then it takes the element at index 0 of [b] - returning b. a now contains the value of b that was stored there, rather than the up-to-date value of b, so our swap is successful (albeit obfuscated).
Related
I'm just curious about one thing. A little example in Javascript
var a = 1;
a = a++;
console.log(a); // 1
var b = 1;
b = ++b;
console.log(b); // 2
var c = 1;
c += 1;
console.log(c); //2
I understand why it works this way in case b and c, but what about a?
At first the code makes an assignment a = a, the value stays the same actually, but then it should (as I see) make increment and increase value a per unit. But this not happening. Why?
var a = 1;
a = a++;
1 is assigned to a
a++ is evaluated as 1
a++ increments a to 2
a = {result of previous evaluation} assigns the 1 to a so it is 1 again
var b = 1;
b = ++b;
1 is assigned to b
++b increments b to 2
++b is evaluated as 2
b = {result of previous evaluation} assigns the 2 to b so it is 2 still
That is how post increment works
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
a = a++;
when a++ executes a is 2 and the expression returns 1 and a gets assigned to 1.
That is the reason you seeing the value before increment.
Note that if you don't assign back, you see the incremented value of a. You in short ovverriding the incremented value here by assigning it back.
var a = 1;
a++;
console.log(a); // 1
It is rumored that to execute ++a, the value in memory increases and then returns. While for a++, first the value is stored in the created temporary variable, then the value of the primary variable is incremented and the value of the temporary variable is returned - thus the "expenses" for creating the temporary variable increase.
In the case of a++ the return value is actually the original value. You’ve assigned the original value to a again.
The a++ returns the value that before self-increase;
While the ++a returns the value that after the self-increase;
so this is why when you call a = a++, a is equal to 1;
a= a++ return the previous value of a before increment it's value.
you can just use a++
The postfix operator returns the original value of a.
So a is increased by the postfix ++ operator but then a is overwritten by assigning the return value of the operator, which is the value before incrementing.
The return value of a of 1 is temporary stored for assingment, then the increment takes place, a has now the value of 2 and then the assingmet of the former stored value happens.
a = a++;
is the same like
a = (temp = a, a++, temp);
var a = 1;
// Here a++ means a = a; then a = a + 1
a = a++; // so you are assign first step value inside a so thats way a = 1
console.log(a); // 1
means you are storing value that time when a++ is equal to a = a. You simply assigning the value of a and replacing the value.
let a = 7;
a = 3;
console.log(a); // output 3
In this case, the value 7 is still inside a memory? I was reading that all primitive data types are immutable.
The value of a is kept in memory, until garbage collection eventually recycles it. What the docs mean by immutable is that you can't directly alter the primative (in this case the integer 7). You can only replace the value.
There are examples on the docs but this is another one
let a = 1;
a.toString() // a is still 1, it cannot be mutated
However, we can assign this to another variable
let a = 1;
let b = a.toString() // b is string "1" and a remains as the integer 1
Or we can replace the value
let a = 1;
a = 10; // a is 10
Short answer: no.
Immutable is about the behaviour of the value, not about what happens when it's no longer being references: you cannot change an immutable value, you have to instead reassign your variable if you want it to point to a new value.
let b = 3;
let a = b; // the variable "a" points to the primitive value 3
b = 7; // the variable "a" still points to the primitive value 3
a = 7; // and now it points to a _different_ value. 7, in this case.
This is in contrast to things like arrays or objects, which you can change as much as you like without needing to reassign:
const obj = {};
const a = obj;
obj.cat = `meow`;
console.log(a); // this will show that "a" is { cat: "meow" }
We just changed the content of the value that "a" points to, without any reassignments.
This question already has answers here:
Javascript a=b=c statements
(6 answers)
Closed 9 years ago.
I saw this code somewhere, but what does it mean? (all a, b, c are defined previously)
var a = b = c;
It quickly assigns multiple variables to a single value.
In your example, a and b are now equal set to the value of c.
It's also often used for a mass assign of null to clean up.
a = b = c = d = null;
Assign c to b.
Assign b to a.
So if I say var a = b = 1;
>>> var a = b = 1;
undefined
>>> a
1
>>> b
1
This means a, b and c are the same reference.
For example:
var c = {hello: "world"};
var a = b = c;
// now all three variables are the same object
It's a shorthand for:
var a;
var b;
b=c;
a=b;
It's meant as a combination of assigning the same value to two or more other variables, and declaring these variables in local scope at the same time.
You can also use this syntax independently of the var declaration:
var a;
var b;
a=b=c;
As gdoron pointed out,
var a = "a";
var b = "b";
a = [b][b = a,0];
Will swap a and b, and although it looks a bit of hacky, it has triggered my curiosity and I am very curious at how it works. It doesn't make any sense to me.
var a = "a";
var b = "b";
a = [b][b = a, 0];
Let's break the last line into pieces:
[b] // Puts b in an array - a safe place for the swap.
[b = a] // Assign a in b
[b = a,0] // Assign a in b and return the later expression - 0 with the comma operator.
so finally it is a =[b][0] - the first object in the [b] array => b assigned to a
Live DEMO
read #am not I am comments in this question:
When is the comma operator useful?
It's his code...
It might help (or hinder) to think of it terms of the semantically equivalent lambda construction (here, parameter c takes the place of element 0):
a = (function(c) { b = a; return c; })(b);
I've made an experiment in JavaScript:
var x=[ "1","2","3","4","5","6"];
c=(b = x)[2] ; //<--- what is this syntax?
alert(b ); // 1,2,3,4,5,6
alert(c ); // 3
I figured it out how it works. It saves me a line of equalization. Still, I was wondering about this strange syntax. How is it called and where can I read about it?
= operator returns a value being assigned so (b = x) returns value of x. That results in x[2] being assigned to c.
(note: "returns x" changed to "returns value of x" according to comments)
See below
b=x assigning the data of x to b and hence you are getting alert as 1,2,3,4,5,6
c=(b=x)[2] is displaying the second content of array i.e. 3.
Below is how c=(b=x)[2] works
c=(b=x)[2]
b=x
c=b[2]
let me know if you need further details...
this happens because of an assigment operator returns value in java script.
Think about it like this:
You make an assignment:
b = x;
Now in object oriented terms, its like invoking a method = on b with an x as an argument
b.=(x)
Here the '=' is like a method name
Now if its the method, why it can't return a value (its return type is other than void)?
Here you can find some additional explanation about this:
Link1
Link2
Hope this helps
var x = ["1","2","3","4","5","6"];
c=(b = x)[2]; // b = x; -> shallow copy of the array
// b -> now hods the array of x
// b[2] -> get the second element of the array
alert(b ); // b is pointing to the array
alert(c ); // c has the 3-rd element (array counting is from 0 - 0,1,2)
b.push("7"); // we add a new item to the array
alert(b); // 1,2,3,4,5,6
alert(x); //1,2,3,4,5,6,7 !
The same code can be written as:
var x = b = ["1","2","3","4","5","6"], c = b[2];
console.log(b); //=> 1,2,3,4,5,6
console.log(c); //=> 3
Or even:
var c = (x = b = ["1","2","3","4","5","6"])[2];
console.log(b); //=> 1,2,3,4,5,6
console.log(c); //=> 3
In var c = (b=x)[2], the parentheses force b=x to execute first. In both cases anyhow, when for example used within a function scope, the statement would create variable b in the global scope, so I would consider it bad practice.