Why does Console return “undefined” on executing the log method ?

Why does Console return “undefined” on executing the log method ?

You've probably noticed some strange behavior if you've ever used console.log() in the browser. that you get "undefined" after printing the expected value to the console.

Getting undefined in console

Have you ever wondered why the console displays "undefined" as a result? Allow me to explain

The fact that a function returns a value to the caller is at the heart of the problem.

The log is a method in Console.log() that outputs a message in the web console; because the log method has no return value, it returns undefined to the caller.

Let's understand this with an example:

Example 1

In the above example, anyFunction has a return statement that returns "my text" so when we call the function, we do not get an “undefined”, because we explicitly returned a value in the above function.

Example 2

In Example 2, anyFunction1 does not return any value to the caller hence undefined is returned by default when we call the function.

Conclusion

When we call Console.log() It does its job of printing the value into the console but has nothing to return, so it returns “undefined” to the caller, resulting in “undefined” on the console.

Share your thoughts in the comments and let me know if there is anything I can do better.

Thank you for reading.