Javascript Boolean Validation [duplicate] - javascript

This question already has an answer here:
(![]+[])[+[]]... Explain why this works
(1 answer)
Closed 4 years ago.
My question is about boolean validation of below expression.
If you run (!+[]+[]+![]) in your JS console, it returns us 'truefalse'.
How is it possible? How does this logic work?

First part !+[] returns true as a Boolean. Second part []+![] is "false" as String. Concatenating Boolean with String converts the result to string and gives you at the end "truefalse".
Here is provided deep explanation to JSFuck
https://github.com/aemkei/jsfuck#how-it-works

Related

What exactly is (alert(1),"") in javascript [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I tried doing google gruyeres XSS challenges (http://google-gruyere.appspot.com/part2), and at the stored AJAX XSS challenge they have the following code part for the JSON response:
all <span style=display:none>"
+ (alert(1),"")
+ "</span>your base
The interesting part is: (alert(1),"")
According to the solution provided, the empty string gets returned. According to my testing, the alert(1) still gets exectued.
Is this some sort of function shorthand, or what would this be called in JS?
Why does it execute the alert, but then return the empty string?
Thank you very much for any help!
Best regards,
Rolf
This is the comma operator. The code executes alert(1), discards its return value, then evaluates "". Since this is the last item in the expression, its value is returned, which is empty string.
The tutorial I linked describes it as follows:
The comma operator in JavaScript evaluates each of its operands. It returns the value of the last operand. Add multiple expressions using the comma operator.

What is this bracket sequence syntax called in JS? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I really don't know what to google to find out the name for this syntax:
(1,2) seems to evaluate to 2
(1,2,3,"cake") seems to evaluate to "cake".
etc
This is sometimes useful in anonymous functions in Array.reduce, where you need to perform a sequence of operations (say an increment) and also return the element on the right.
But where can I read about it and what's it called?
It's just the comma operator: it evaluates each of its operands (from left to right) and returns the value of the last operand.

How can '+myVar;' be a valid syntax [duplicate]

This question already has answers here:
Explain +var and -var unary operator in javascript
(7 answers)
Closed 6 years ago.
I just got a bug that took some of my time to spot my searching filters weren't working because of the following code :
queryObject.search='valid==true';+searchQuery;
The good syntax is to mive the ';' in the string :
queryObject.search='valid==true;'+searchQuery;
The reasn why i didn't spot that is because the earlier line of code didn't triggered any javascript console error. So it seems it's a valid syntax.
So here is my question, how can this be a valid syntax ?
+something is an expression using the plus unary operator.
Its general purpose is to convert a value, for example a string, to a number.
+ is unary operator, which tries to get numeric value from variable.
There is a thread about it.

Why javascript indexOf "empty string" always returning zero [duplicate]

This question already has answers here:
indexOf empty string is zero. why?
(1 answer)
Java String.indexOf and empty Strings
(7 answers)
Closed 8 years ago.
Why javascript indexOf method always return 0 for empty string. If i used any string indexOf("") always returns zero. Any reason for this. Explain the reason briefley.
var str="jquery";
var index=str.indexOf("") \\ Always returns zero for any string.
Thanks in advance.

Length regex tests true on null [duplicate]

This question already has answers here:
Is it a bug in Ecmascript - /\S/.test(null) returns true?
(2 answers)
Closed 9 years ago.
I need to verify that an input is between 1 and 512 characters long. I'm using the standard length-check regex of /^.{1,512}$/. When I run
/^.{1,512}$/.test(null)
it returns true. How do I get a length-check regex to fail against null? And why does this test true against null in the first place?
EDIT: Leaving this here since it's more googleable in my case than the earlier question, but per here, the problem is that the regex coerces null into 'null' before testing.
I would test with a regex like this:
var myregexp = /^(?!null).{1,512}$/m;;

Categories

Resources