Why ++var++ is not a legal Javascript expression? [duplicate] - javascript

This question already has an answer here:
Increment a number by prefix and postfix operator
(1 answer)
Closed 5 years ago.
I was reading about Javascript operators precedence over here and got curious why I can't write something like this:
let num = 1;
++num++;
Which gets Uncaught ReferenceError: Invalid left-hand side expression in prefix operation error. But why is that? :)

It evaluates as
++(num++)
so, the expression
num++
returns a number, not the variable, because it is a primitive value. The added plusses, throws an exception, because a primitive value is not a variable and an assigment is not possible.

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.

Why executing ('Hello','world!) returns 'world!'? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
How does the comma operator work in js? [duplicate]
(1 answer)
Closed 3 years ago.
I'm learning programming with JavaScript, and I just don't know what's happening here:
let myVar = ('Hello','World!');
console.log(myVar);
The output is:
World!
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
This means that the expression 'Hello','World!' evaluates to 'World!'
You're setting the variable to the string "Hello", then setting it to "World". The same thing happens if you set a list of variables inside a parenthetical.
const myVar = (1,2,3,4,5,6);
console.log(myVar);
//6
Where as I think what you were trying to do was use a grouping operator like +.
const myVar = ("Hello, " + "World")
console.log(myVar)
//"Hello, World"

Why is (date1, date2) a valid expression in javascript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 4 years ago.
I know, weird question. I accidentally pasted an expression in the the Chrome developer tools console but copied it without the function call so instead of:
dayDiff(date1, date2)
I just ran
(date1, date2)
with the variables in parenthesis separated by a comma, which simply returns the value of date2. I'm trying to understand what the engine is interpreting this expression as. I would've expected this to be a syntax error.
It is comma operator. It simply evaluates both arguments and returns the RHS value.

JavaScript increment (++) operator on both sides of an argument? [duplicate]

This question already has an answer here:
Increment a number by prefix and postfix operator
(1 answer)
Closed last year.
I'm struggling to understand the behaviour of Javascript's increment operator, and more specifically, why certain cases fail.
Why does adding increment operator on both sides of an argument fail?
EXAMPLE:
let a = 1;
++a++;
This returns a horrible error stating that:
ReferenceError: Invalid left-hand side expression in prefix operation
What does this mean, and should I be worried?
The increment operators work on variables, not on expressions. You can't increment a numeric expression:
3++ // Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
The reason for this is that it must increment the value, then save it back to the variable. If you gave it any old numeric expression, what would it assign the result to?
One of the two operators will work, but it returns the result of the operation, which is an expression, not a variable:
++(a++)
The first operator, a++, will increment a, and return the result: 2. The second operator is then trying to perform the increment on the value 2, which is invalid syntax.
That code could be rewritten as: ++(a++) which would translate to ++(1), and then ++1, which is not a valid expression.

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.

Categories

Resources