I am trying to work out some obfusicated code by reading it, and I was doing pretty well until a came across this:
a = a && "*"
Also, if anybody knows where I can look to find out what these uncommon lines of code do, that would be very helpful. They are all shortened and are sorts of things like this and
a = a || b
(I know what that one does)
a is truthy, it assigns
a is falsy, it remains untouched.
&& has short-circuit semantics: A compound expression
(e1) && (e2)—where
e2 are arbitrary expressions themselves—evaluates to either
e1evaluates to false in a boolean context—
e2is not evaluated
e1evaluates to true in a boolean context
This does not imply that
e2 and the entire expression
(e1) && (e2) need evaluate to true or false!
In a boolean context, the following values evaluate to
false as per the spec:
All1 other values are considered
The above values are succinctly called "falsy" and the others "truthy".
Applied to your example:
a = a && "*"
According to the aforementioned rules of short-circuit evaluation for
&&, the expression evaluates to
a is falsy, which is then in turn assigned to
a, i.e. the statement simplifies to
a = a.
a is truthy, however, the expression on the right-hand side evaluates to
*, which is in turn assigned to
As for your second question:
(e1) || (e2) has similar semantics:
The entire expression evaluates to:
©2020 All rights reserved.