Why console.log() returns undefined?

idrisso

Mohamed Idris

Posted on August 1, 2022

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

💖 💪 🙅 🚩
idrisso
Mohamed Idris

Posted on August 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related