Console returns undefined [duplicate]

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):

  1. 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.
  2. 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).
  3. Exit the execution context funcCtx, restoring the previous execution context.
  4. If result.type is throw then throw result.value.
  5. If result.type is return then return result.value.
  6. Otherwise result.type must be normal. Return undefined.

Leave a Comment