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;
Related
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I need some help understand why a relationship is created between arrays when I replicate an array by reference. Take the following example:
let a = [1,2,3];
let b = a;
b.push(4);
console.log(b); // [1,2,3,4]
console.log(a); // [1,2,3,4]
a === b; // true
For some reason, {a} changes when I modify {b}, when I expected them to be independant arrays.
This kind of relationship is not true for variables, it appears. Take the following example:
let a = 'test';
let b = a;
b = 'test1';
console.log(b); // "test1"
console.log(a); // "test"
a === b; // false
Does anyone know the reasoning behind the behavior for arrays?
Arrays are copied by reference so when you write:
let b = a;
You are referencing a in b.
The correct way:
let b = [...a]
or
let b = a.slice(0);
This question already has answers here:
Assign two variables to the same value with one expression? [duplicate]
(5 answers)
Closed 4 years ago.
In this situation:
(function() {
const a = b = 10;
})();
console.log('a:', a); // ReferenceError: a is not defined
console.log('b:', b); // 10
The b gets defined as a var in the global scope. Is it possible o do this equals trick but make b a const as well in the same scope? Just like a?
You could set b as a parameter to your IIFE function. That way b won't be accessible outside of function scope.
(function(b) {
const a = b = 10;
console.log(b)
})();
console.log('b:', b);
I don't think so. This "trick" is possible because b = 10 returns 10. However, const b = 10 returns undefined.
Why not just use:
const a = 10;
const b = a;
?
It's not slower, and it's more readable, even if you could nest the assignments I don't really see a reason to do it
This question already has answers here:
assignment in javascript and the var keyword
(4 answers)
Closed 8 years ago.
I have found a phenomenon that confused me when I run simple javascript code from the browser console(Chrome & Firefox).
When I typed say
>var a = "a"
The browser will return a string
>"undefined"
but if I just typed
>a = "a"
The browser will return the string
>"a"
Why is this case?
If you write
alert(var a = 'a')
You get a syntax error, var is part of the javascript syntax, it doesn't return anything.
The a = 'a' portion, however, does return something.
You can do var a = b = c = d = 'e';
And the d = 'e' returns e, which gets fed into the c=d which is really c = 'e', etc. Once you get to the var it stops returning the value.
If you type var a; you get undefined. var a = 'b' is essentially just shorthand for var a; a = b;
The console is showing the result of the evaluated expression. Declaring the variable and assigning it at the same time with
var a = 'a'
does not return anything, so you get undefined. The result of just the assignment part
b = 'b'
returns the value, so you see it in the console.
var a = b = c = d = 'foo';
That returns undefined, although several variables have been set. The real purpose of the expression was to define the scope of the variables, setting the value was just a bit of shorthand.
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).
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);