I was messing around with an AST tree parser and I saw that a ReturnStatement
can have multiple expressions
. As you can see in the following snippet there are multiple values after the return statement and yet the code get compiled and run successfully (its the last value that gets returned).
function test() {
return 1, 2, 3;
}
console.log(test());
AST Form:
{
"type": "ReturnStatement",
"start": 13,
"end": 24,
"argument": {
"type": "SequenceExpression",
"start": 20,
"end": 23,
"expressions": [{
"type": "Literal",
"start": 27,
"end": 28,
"value": 1,
"raw": "1"
}, {
"type": "Literal",
"start": 30,
"end": 31,
"value": 2,
"raw": "2"
}, {
"type": "Literal",
"start": 33,
"end": 34,
"value": 2,
"raw": "3"
}]
}
}
What is the point of this feature and/or bug?
When would you ever want to use this syntax?