Why does a function without a return statement return undefined?
By spec. If a function has no return or a return with no expression, it returns undefined. Always add return explicitly when you need to send a value back to the caller.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Common Function Bugs in JavaScript and How to Fix Them
Because the method is detached from obj when passed as a reference. setTimeout calls it with this as the global object (or undefined in strict mode). Fix it with .bind(obj), an arrow wrapper, or () => obj.method().
Use let instead of var so each iteration gets a fresh binding, or wrap the body in an IIFE that captures the current value of i as a parameter.
Because the braces are interpreted as a block body, and the object literal looks like a label statement. Wrapping the object in parentheses (n) => ({ x: 1 }) makes it an expression body that returns the object.
Still have questions?
Browse all our FAQs or reach out to our support team
