1

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.

xr280xr
  • 12,621
  • 7
  • 81
  • 125

1 Answers1

2

It is comma operator. It simply evaluates both arguments and returns the RHS value.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
bipll
  • 11,747
  • 1
  • 18
  • 32
  • It *is* a comma operator by why link to the C++ docs on it, instead of any JS sources? – VLAZ Sep 14 '18 at 21:32
  • Ah, got it. I wasn't aware of that (in JS, or C++), but my mind went to crazier possibilities. Thanks. – xr280xr Sep 14 '18 at 21:33
  • @vlaz JS stems from the C family, so why not? – bipll Sep 14 '18 at 22:45
  • Because 1. there are [already JS docs for this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) 2. Even though it comes from C (which is not 100% true, as it also has roots in functional programming, namely Schema) the documentation there doesn't mean it's correct for JS. In fact, according to the C++ reference, you can override a comma operator, which is certainly not possible for JS. 3. It suggests to newcomers that C++ reference is valid for anything JS related. 4. You didn't offer any reason *for* using this source. – VLAZ Sep 14 '18 at 22:57