Reputation: 1814
I have a simple function that returns array of items.
function arrayFromValue(...item) {
return item;
}
console.log(arrayFromValue(1, 2, 3));
The function works fine but, when wrote the code like this:
function arrayFromValue(...item) {
return
item;
}
console.log(arrayFromValue(1, 2, 3));
it outputs undefined
Upvotes: 2
Views: 49
Reputation: 15166
The reason behind that is Automatic Semicolon Insertion. Read from the documentation:
The return statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
So your code which returns undefined
is equivalent with:
function arrayFromValue(...item) {
return;
}
After return
statement the rest of the code will be ignored.
I hope that helps!
Upvotes: 3