Edit: At the bottom I've added what I've figured out since I wrote this.
Today I sat down and figured out why you get this result in JavaScript
...
For further actions, you may consider blocking this person and/or reporting abuse
In some cases it does, but not in this case. The reason you see behaviour that may appear to be right-to-left evaluation is because of operator precedence, where the unary
!
has a higher precedence than the binary+
.+
actually evaluates from left to right, so more verbosely parenthesized, it looks like this:Evaluating this parenthesized form step-by-step, we get this:
The first + is unary, right?
Indeed it is.
If I'm not sure what's going on with the JS code, I use ASTexplorer to look at the code with the compiler's eyes. It converts a code string into the tree of instructions to be executed by the compiler step by step.
But sometimes text representation of a tree is not expressive enough, so I built a little tool which visualizes an AST tree.
For
(! + [] + [] + ![]).length
AST in graph form looks like this:In runtime calculations starts from the bottom left and flow to the top.
love this!
This is also equal to 9 but for a very different reason
This one too but for yet another reason
Standing on the shoulders of giants.
You can stand on your own shoulders this was perfect
Interesting breakdown. I might have missed something but I think "truefalls" should be "truefalse".
Indeed it should =D That's dyslexia for ya.
Ain't that the truef.
It is a great observation , typecasting in JS is weird. This is the reason I love this platform.
AWESOME INTERVIEW QUESTION!!!
jajaja many brains will explode
Totally greatly interesting.
Thank you for your great post.
The behaviors seem a bit strange and are beautiful 😆
Hi.
This is very well documented in the Boolean coercion abstract operation by ECMAScript language.
So, there are two operations that you may check out.
Is this ease of programming?
Does this have anything to do with the esoteric version of JS? en.m.wikipedia.org/wiki/JSFuck