Multiple left-hand assignment with JavaScript
Solution 1:
Actually,
var var1 = 1, var2 = 1, var3 = 1;
is not equivalent to:
var var1 = var2 = var3 = 1;
The difference is in scoping:
function good() {
var var1 = 1, var2 = 1, var3 = 1;
}
function bad() {
var var1 = var2 = var3 = 1;
}
good();
console.log(window.var2); // undefined
bad();
console.log(window.var2); // 1. Aggh!
Actually this shows that assignment are right associative. The bad
example is equivalent to:
var var1 = (window.var2 = (window.var3 = 1));
Solution 2:
Assignment in javascript works from right to left. var var1 = var2 = var3 = 1;
.
If the value of any of these variables is 1
after this statement, then logically it must have started from the right, otherwise the value or var1
and var2
would be undefined.
You can think of it as equivalent to var var1 = (var2 = (var3 = 1));
where the inner-most set of parenthesis is evaluated first.
Solution 3:
var var1 = 1, var2 = 1, var3 = 1;
In this case var
keyword is applicable to all the three variables.
var var1 = 1,
var2 = 1,
var3 = 1;
which is not equivalent to this:
var var1 = var2 = var3 = 1;
In this case behind the screens var
keyword is only applicable to var1
due to variable hoisting and rest of the expression is evaluated normally so the variables var2, var3
are becoming globals
Javascript treats this code in this order:
/*
var 1 is local to the particular scope because of var keyword
var2 and var3 will become globals because they've used without var keyword
*/
var var1; //only variable declarations will be hoisted.
var1= var2= var3 = 1;
Solution 4:
a = (b = 'string is truthy'); // b gets string; a gets b, which is a primitive (copy)
a = (b = { c: 'yes' }); // they point to the same object; a === b (not a copy)
(a && b)
is logically (a ? b : a)
and behaves like multiplication (eg. !!a * !!b
)
(a || b)
is logically (a ? a : b)
and behaves like addition (eg. !!a + !!b
)
(a = 0, b)
is short for not caring if a
is truthy, implicitly return b
a = (b = 0) && "nope, but a is 0 and b is 0"; // b is falsey + order of operations
a = (b = "b is this string") && "a gets this string"; // b is truthy + order of ops
JavaScript Operator Precedence (Order of Operations)
Note that the comma operator is actually the least privileged operator, but parenthesis are the most privileged, and they go hand-in-hand when constructing one-line expressions.
Eventually, you may need 'thunks' rather than hardcoded values, and to me, a thunk is both the function and the resultant value (the same 'thing').
const windowInnerHeight = () => 0.8 * window.innerHeight; // a thunk
windowInnerHeight(); // a thunk