DEV Community

Cover image for Why console.log() returns undefined?
Mohamed Idris
Mohamed Idris

Posted on

3 1

Why console.log() returns undefined?

console.log() is a function. And a function is always going to return some value back to the caller. If a return value is not specified, then the function will return back undefined, as in console.log().

For example, copy and paste the below code in the console:

function isThisWorking(input) {
    console.log("Printing: isThisWorking was called and " + input + " was passed in as an argument.");

    return "I am returning this string!";
}
isThisWorking(3);
Enter fullscreen mode Exit fullscreen mode

So, isThisWorking function will return back "I am returning this string!" in the console, because that's what the function is returning.

But, If we removed the return, it will return back undefined:

function isThisWorking(input) {
    console.log("Printing: isThisWorking was called and " + input + " was passed in as an argument.");
}

isThisWorking(3);
Enter fullscreen mode Exit fullscreen mode

Credits

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay