This question already has an answer here:
(![]+[])[+[]]... Explain why this works
(1 answer)
Closed 8 years ago.
I realize that this is intended as a joke, but someone posted this and it alerts "fail" in the browser, what is going on that makes this happen?
alert((![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]);
Here is a good blog post about this topic: http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html
You basically create strings ("false","undefined" and others) and again use addition of true's to get the indexes.
For example the first letter (f):
(![]+[])[+[]]
You use ![] to get false, and add [] to convert it into a string. The content within the parenthesis is now "false". You then access it like an array, and ยด+[]` converts to 0 (false as a number).
Related
This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 3 years ago.
code snippet of unirest
As you can see above, arrayIndexOf refers to a function. When invoking the arrayIndexOf function,
is it necessary to put "~" before "arrayIndexOf"? I try to change "~arrayIndexOf(value, field)" to "arrayIndexOf(value, field)". And it works as the same? Is there anything I miss?
In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right
before an indexOf() to do a boolean check (truthy/falsy) on a string.
On its own, indexOf() returns the index number of a String object
passed in. So if -1 is returned it will be turned into 0 which is
falsy.
Source: https://wsvincent.com/javascript-tilde/
This question already has an answer here:
All characters in a string must match regex
(1 answer)
Closed 2 years ago.
I have this code and I am wondering why it logs true:
console.log(/[A-Za-z0-9-_:]/.test('zoom^Bar'));
I would assume the caret character would make the regex fail, why is it passing?
Just want to say this question is straightforward but might help others and the "duplicate" question doesn't really like a simple dupe to me.
Try this:
console.log(/^[A-Za-z0-9-_:]+$/.test('zoom^Bar'))
This question already has answers here:
How to convert a String containing Scientific Notation to correct Javascript number format
(8 answers)
Closed 5 years ago.
I have a string in response "2.222222522879E12",
When I do
parseFloat("2.222222522879E12")
2222222522879
But the expected value should be 2.22,
Whats happening ?
And how can I fix it
You're going to get 2.222222522879* 10^12 with that expression as E means exponent, or to the power of
As per your edit asking how to fix:
use the Number.prototype.toPrecision() function. Here is the documentation
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
I know 1 is not an object, but when I type 1..toString(), it returns "1" in the console. Why is that?
Because the JavaScript parser assumes that 1. must be followed only by one or more digits to represent a float number. Using parentheses works: (1).toString().
Because it is interpreting 1. as the number. When you have 1.toString(), it is the same as saying (1.)toString(). Therefore 1..toString() is the same as (1.).toString()
The reason why the below works is:
1..toString()
The 1.. is considered as a floating point number. The console expects something like:
1.0
1.5
Or something. If you are giving something like:
1.toString();
The above is not a valid number. That's the reason. So to make the above work, you need a parenthesis to tell that the number is completed:
(1).toString();
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I was just trawling through the QUnit source code (1.12.0) and came across a line that confused me. I've done a bit of googling and haven't been able to come up with a reason for it.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 520
result = !!result;
A similar thing appears further on in the code, except instead of storing the result in itself, it's storing the double negated variable in JSON.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 957
result: !!result
As ! negates, I assume !! will negate then negate again, thus ending up with exactly what you started with. In which case, what is achieved by setting a variable equal to itself, negated twice? (Or in the latter example, returning itself negated twice instead of just returning itself.)
!! is used to convert the value to the right of it to its equivalent boolean value.
Also check this related Thread.