- Joined
- Sep 16, 2012
- Messages
- 453
- Reaction score
- 211
- First Language
- English
- Primarily Uses
- RMMV
Recently I've been reading "Expert JavaScript" by Mark E. Daggett (which I highly recommend) when I came across a chapter that focused on some interesting programmatic jargon. Some of this jargon I can not see as being particularly useful in production code (except where it can shorten some common operations). However I believe one can use this to expand their knowledge on how JS functions. I encourage you to try and figure out what is happening behind each example and what it may equal. I will also try to explain in spoilers the process for each example I show (note this is based off my own observations and from my reading please feel free to correct me if I am wrong).
Using Bitwise operations for math (math is not always what it appears):
-~5
~-5
2*~8
2*~-8
2/~9
2/~-9
Cryptic Coercion:
console.log((![]+[])[+[!+[]+!+[]]]+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[+[]]])
Using Bitwise operations for math (math is not always what it appears):
-~5
~-5
2*~8
2*~-8
2/~9
2/~-9
-~5 adds one to the 5 result 6.
~-5 subtracts one from the 5 result 4.
2*~8 invert the 8 to a negative number, subtract one you now have -9 multiply by 2 you get -18.
2*~-8 subtract 1 and you get 7 multiply by 2 and you now have 14.
2/~9 invert the 9 to a negative number, subtract one you now have -10, take the 2 and divide -10 from it you now have -0.2.
2/~-9 subtract 1 from the 9 leaving you with 8, take the 2 and divide 8 from it you now have 0.25.
~-5 subtracts one from the 5 result 4.
2*~8 invert the 8 to a negative number, subtract one you now have -9 multiply by 2 you get -18.
2*~-8 subtract 1 and you get 7 multiply by 2 and you now have 14.
2/~9 invert the 9 to a negative number, subtract one you now have -10, take the 2 and divide -10 from it you now have -0.2.
2/~-9 subtract 1 from the 9 leaving you with 8, take the 2 and divide 8 from it you now have 0.25.
Cryptic Coercion:
console.log((![]+[])[+[!+[]+!+[]]]+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[+[]]])
So what in the world is happening up their you may be asking... (And do note that I can not see any real scenario where this should be used I just found it quite interesting)
Well in this example we have
(![]+[]) // ![] is false which then gets concatenated to a string with the + []
[+[!+[]+!+[]]] //+!+[] is equal to 1 because !+[] equals true this gets added to !+[] giving us 2 which in turn gets placed in an array.
//so now we can turn this into the following code for easier translation
"false"[2]
//this gives use the letter "l"
+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]
//like before the first part gives use the string "false"
//the second part gives us 4 wrapped in an array
"false"[4]
//this gives us the letters "le" now
"false"[4]
//repeating this one more time gives us "lee"
+(!![]+[])[+[+[]]]
//The first part gets notted twice giving us the string "true"
//the second part givs us the number 0 wrapped in an array
"true"[0]
//this gives us the letter "t" we now have "leet"
What other weird brain exercises have you found in JS that may or may not have use in production code?
Well in this example we have
(![]+[]) // ![] is false which then gets concatenated to a string with the + []
[+[!+[]+!+[]]] //+!+[] is equal to 1 because !+[] equals true this gets added to !+[] giving us 2 which in turn gets placed in an array.
//so now we can turn this into the following code for easier translation
"false"[2]
//this gives use the letter "l"
+(![]+[])[+[]+!+[]+!+[]+!+[]+!+[]]
//like before the first part gives use the string "false"
//the second part gives us 4 wrapped in an array
"false"[4]
//this gives us the letters "le" now
"false"[4]
//repeating this one more time gives us "lee"
+(!![]+[])[+[+[]]]
//The first part gets notted twice giving us the string "true"
//the second part givs us the number 0 wrapped in an array
"true"[0]
//this gives us the letter "t" we now have "leet"
What other weird brain exercises have you found in JS that may or may not have use in production code?
