-1

What's going on here using JavaScript?

var c = +(3,13);    

I got c equals to 13. Why?

1 Answers1

5

The comma functions as an expression separator operator. The value of a sequence of expressions separated by commas is the value of the last expression. Thus 3, 13 has the value 13. That's surrounded with parentheses and the unary + operator, neither of which will affect that value.

The comma operator is useful in only a small number of situations, generally involving expressions with side effects. In particular, your example is basically pointless. However, there are situations where some statement syntax allows for a single expression, so the comma operator lets you sneak in more than one.

Pointy
  • 405,095
  • 59
  • 585
  • 614