Why doesn't my arrow function return a value?
up vote
11
down vote
favorite
I have an arrow function that looks like this (simplified):
const f = arg => { arg.toUpperCase(); };
But when I call it, I get undefined:
console.log(f("testing")); // undefined
Why?
Example:
const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));(Note: This is meant to be a clean, canonical dupetarget for the specific issue with arrow functions above.)
javascript ecmascript-6 arrow-functions
add a comment |
up vote
11
down vote
favorite
I have an arrow function that looks like this (simplified):
const f = arg => { arg.toUpperCase(); };
But when I call it, I get undefined:
console.log(f("testing")); // undefined
Why?
Example:
const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));(Note: This is meant to be a clean, canonical dupetarget for the specific issue with arrow functions above.)
javascript ecmascript-6 arrow-functions
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
I have an arrow function that looks like this (simplified):
const f = arg => { arg.toUpperCase(); };
But when I call it, I get undefined:
console.log(f("testing")); // undefined
Why?
Example:
const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));(Note: This is meant to be a clean, canonical dupetarget for the specific issue with arrow functions above.)
javascript ecmascript-6 arrow-functions
I have an arrow function that looks like this (simplified):
const f = arg => { arg.toUpperCase(); };
But when I call it, I get undefined:
console.log(f("testing")); // undefined
Why?
Example:
const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));(Note: This is meant to be a clean, canonical dupetarget for the specific issue with arrow functions above.)
const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));const f = arg => { arg.toUpperCase(); };
console.log(f("testing"));javascript ecmascript-6 arrow-functions
javascript ecmascript-6 arrow-functions
asked Aug 18 '17 at 10:54
community wiki
T.J. Crowder
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
15
down vote
accepted
When you use the function body version of an arrow function (with {}), there is no implied return. You have to specify it. When you use the concise body (no {}), the result of the body expression is implicitly returned by the function.
So you would write that either with an explicit return:
const f = arg => { return arg.toUpperCase(); };
// Explicit return ^^^^^^
or with a concise body:
const f = arg => arg.toUpperCase();
Examples:
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));Slightly tangential, but speaking of {}: If you want the concise arrow's body expression to be an object initializer, put it in ():
const f = arg => ({prop: arg.toUpperCase()});
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
accepted
When you use the function body version of an arrow function (with {}), there is no implied return. You have to specify it. When you use the concise body (no {}), the result of the body expression is implicitly returned by the function.
So you would write that either with an explicit return:
const f = arg => { return arg.toUpperCase(); };
// Explicit return ^^^^^^
or with a concise body:
const f = arg => arg.toUpperCase();
Examples:
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));Slightly tangential, but speaking of {}: If you want the concise arrow's body expression to be an object initializer, put it in ():
const f = arg => ({prop: arg.toUpperCase()});
add a comment |
up vote
15
down vote
accepted
When you use the function body version of an arrow function (with {}), there is no implied return. You have to specify it. When you use the concise body (no {}), the result of the body expression is implicitly returned by the function.
So you would write that either with an explicit return:
const f = arg => { return arg.toUpperCase(); };
// Explicit return ^^^^^^
or with a concise body:
const f = arg => arg.toUpperCase();
Examples:
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));Slightly tangential, but speaking of {}: If you want the concise arrow's body expression to be an object initializer, put it in ():
const f = arg => ({prop: arg.toUpperCase()});
add a comment |
up vote
15
down vote
accepted
up vote
15
down vote
accepted
When you use the function body version of an arrow function (with {}), there is no implied return. You have to specify it. When you use the concise body (no {}), the result of the body expression is implicitly returned by the function.
So you would write that either with an explicit return:
const f = arg => { return arg.toUpperCase(); };
// Explicit return ^^^^^^
or with a concise body:
const f = arg => arg.toUpperCase();
Examples:
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));Slightly tangential, but speaking of {}: If you want the concise arrow's body expression to be an object initializer, put it in ():
const f = arg => ({prop: arg.toUpperCase()});
When you use the function body version of an arrow function (with {}), there is no implied return. You have to specify it. When you use the concise body (no {}), the result of the body expression is implicitly returned by the function.
So you would write that either with an explicit return:
const f = arg => { return arg.toUpperCase(); };
// Explicit return ^^^^^^
or with a concise body:
const f = arg => arg.toUpperCase();
Examples:
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));Slightly tangential, but speaking of {}: If you want the concise arrow's body expression to be an object initializer, put it in ():
const f = arg => ({prop: arg.toUpperCase()});
const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));const f1 = arg => { return arg.toUpperCase(); };
console.log(f1("testing"));
const f2 = arg => arg.toUpperCase();
console.log(f2("testing"));edited Aug 18 '17 at 11:55
community wiki
3 revs
T.J. Crowder
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45754957%2fwhy-doesnt-my-arrow-function-return-a-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown