Console returns undefined [duplicate]
Solution 1:
If I've understood your question correctly, it's because you are not explicitly returning anything from the function. When you don't return a value from a function, it implicitly returns undefined
.
For example:
function example() {}
console.log(example()); //undefined
This is defined in the [[Call]]
internal method specification (relevant points in bold):
- Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
- Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
- Exit the execution context funcCtx, restoring the previous execution context.
- If result.type is throw then throw result.value.
- If result.type is return then return result.value.
- Otherwise result.type must be normal. Return undefined.