I'm running this code:
const myobj ={
mynum: ()=>{console.log("1")},
}
console.log(myobj.mynum())
It returns: 1 undefined
Where is "undefined" comes from? What's the main purpose to create a method like that? Is it even a method?
I'm running this code:
const myobj ={
mynum: ()=>{console.log("1")},
}
console.log(myobj.mynum())
It returns: 1 undefined
Where is "undefined" comes from? What's the main purpose to create a method like that? Is it even a method?
The undefined comes from the fact that the function doesn't return anything. Take for example
() => 3
That function will return a 3, notice that there isn't any {}
around the function, and thus the last value evaluated will be returned.
Then take for instance this function:
() => { return 3; }
That will also return a 3 because we have explicitly added a return statement.
In your case you would need to add an explicit return statement if you want to use the {}
braces, however console.log
also returns undefined, so in your case either way would produce undefined
as a result.