javascript - Why doesn't my arrow function return a value? -
i have arrow function looks (simplified):
const f = arg => { arg.touppercase(); }; but when call it, undefined:
console.log(f("testing")); // undefined why?
example:
const f = arg => { arg.touppercase(); };  console.log(f("testing"));(note: meant clean, canonical dupetarget specific issue arrow functions above.)
when use function body version of arrow function (with {}), there no implied return. have specify it. when use concise body (no {}), result of body expression implicitly returned function.
so write either explicit return:
const f = arg => { return arg.touppercase(); }; // explicit return ^^^^^^ or 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, speaking of {}: if want concise arrow's body expression object initializer, put in ():
const f = arg => ({prop: arg.touppercase()}); 
Comments
Post a Comment