I got hold of some obfuscated JavaScript code. I tried to understand it, and doing this, I typed pieces of it in the console. I can't understand why
> ((!!+[]+"")[+!![]])
< "a"
Why is ((!!+[]+"")[+!![]])
equal to "a"
in JavaScript? Is there some other code snippets to get others letters?
I guess it's something to do with automatic casting.
The eval () is a function property of the global object in JavaScript. The eval () function accepts a string as an argument, evaluates it as an expression, and returns the result. string: It takes a string which is a JavaScript expression, variable, statement, or sequence of statements.
Safely evaluate expressions in JavaScript in a simple yet powerful way, by using a Parsing Expression Grammar Recently, in a front-end project I was working on, the client needed to be able to define calculations by entering formulas using numbers, variables, and functions. The problem: how can you validate and (safely) evaluate such expressions?
It can also be made to use those Big Number libraries for arithmetic in case you are dealing with numbers with arbitrary precision. I went looking for JavaScript libraries for evaluating mathematical expressions, and found these two promising candidates: JavaScript Expression Evaluator: Smaller and hopefully more light-weight.
In JavaScript, an expression is simply a way of asking a question that gets answered with a value — and always a single value. This is an expression: Here’s what’s happening above: You ask: “JavaScript, what is 5 + 5?” JavaScript: “5 + 5 evaluates to 10”.
( ( !!+[] + "" ) [ +!![] ] )
( ( !!0 + "" ) [ +true ] )
( ( false + "" ) [ +true ] )
( ( "false" ) [ 1 ] )
( "false"[1] )
( "a" )
Is there some other code snippets to get others letters ?
You can play with the same concept to get all the letters from "true", "false", "undefined", "NaN"...
You should work on operator precedence and type castings in JavaScript:
!!+[] // Is falsey. this is same for !!+0 or !!+""
false + "" // Is "false". as 5+"" is "5".
![] // Is falsey.
!false // Is true
+true // Is equal to 1. +[] = 0, +false = 0
And at least,
"false"[1] // Is "a"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With