0
function rrr() {
    return (1,2,4);
}

n= rrr();
console.log(n);

when running this code I got the output 3,but I expected 1,2,3 as an output someone please explain this why the output is last element 3 instead of (1,2,3)?

sudhar
  • 1
  • 1
    Actually ,the output is 4 not 3 – flyingfox Oct 29 '22 at 07:03
  • 1
    Perhaps you intended `return [1,2,3]`? The value of `(1,2,3)` is the value of the *last* operand in the expression, which is 3. See the [manual](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) – Nick Oct 29 '22 at 07:05

1 Answers1

0

Comma operator in JavaScript combines multiple expressions, evaluates the expressions from left to right, and returns the value of farther right expression.

Due to the same reason, let x = (1,2,3); assigns value 3 to variable x, let y = (1,2,5-1) assigns value 4 to variable y.

Wazeed
  • 1,230
  • 1
  • 8
  • 9