Comma separated number/function in parenthesis in JavaScript? [duplicate] - javascript

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I read a line from doT.js:
var global = (function(){ return this || (0||eval)('this'); }());
After it was minified:
l=function(){return this||(0,eval)("this")}();
So what is the (0,eval), I mean what does the comma do?
I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.

The comma operator evaluates both and always returns the last. Much like you said.
You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

Related

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.

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: parenthesis surrounding comma separated values [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I'm trying to get my head around the reason JS picks the last element (8):
var values = (null, 7, null, 8);
console.log(values);
// Output: 8
console.log and alert behaves the same so I assume it's something to do with the language itself.
From the first line of the docs:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
This one also explained beautifully link
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand

SyntaxError: Number.toString() in Javascript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
Why do I get an error on this, in Javascript:
10000000.toString();
You can see an example in here:
http://jsbin.com/kagijayecu/1/edit?js,console
It's because the JS parser is expecting more digits after the "." rather than a method name, e.g. 1000000.0, and in fact 1000000.0.toString() will work as expected.
wrap it inside () like this (10000000).toString()
When JS parse meets dot after digit it expects floating-point literal, e.g. 1000000.0

How does JavaScript interpret indexing array with array? [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
Why is this not a SyntaxError error (a bad legacy or a purposeful feature)? (A possible duplicate, however I wasn't able to find an answer here.)
Update: Why the contents of the square brackets are treated as an expression?
The first part:
[1,2,4,8]
is interpreted as an array literal. The second part:
[0,1,2,3]
is interpreted as square bracket notation to access a member of the array. The contents of the square brackets are treated as an expression, which is seen as a sequence of comma separated values:
0,1,2,3 // or (0,1,2,3) as an independent expression
That expression returns the last value, so is effectively:
[1,2,4,8][3] // 8

Categories

Resources