I have an add.js I do this it's valid
const add = (a, b) => {
return a + b;
}
export default add;
but why can't I do this
export default add(a, b) => (a + b)
? I will get add is undefined.
another file i have
import add from './add';
I have an add.js I do this it's valid
const add = (a, b) => {
return a + b;
}
export default add;
but why can't I do this
export default add(a, b) => (a + b)
? I will get add is undefined.
another file i have
import add from './add';
If you just want to export the function directly as the default, you literally just do that (no add
):
export default (a, b) => a + b;
The import will determine the identifier used to reference it.
If you want the function to have a name (e.g., a name
property populated with something other than the default value), you'd either make it a function
function:
export default function add(a, b) { return a + b; };
or do it in two parts as you did originally:
const add = (a, b) => a + b;
export default add;
(Note that the ()
around a + b
in the arrow function are unnecessary, although of course you may choose to have them as a matter of style.)
Because this is not a proper way to declare variable/constant in JS. Only var/constant defined properly gets space (memory).
Therefore, you need to do like this
const add;
or
let add;
or
var add;
May be my explanation was not proper but I meant when you are exporting even in that case you need to use const, var or let for proper declaration if you have not declared a variable already.