I Don’t Trust JavaScript. Here’s Why console.log("Hello") Returned undefined

Ishan Bagchi - Feb 24 - - Dev Community

As a developer, I’ve seen some bizarre things, but nothing made me question reality more than this:

console.log("Hello"); 
// Output: Hello
//         undefined
Enter fullscreen mode Exit fullscreen mode

Wait… what? Why is JavaScript showing undefined after logging "Hello"? Did I break something? Is the universe glitching?

Turns out, JavaScript isn’t broken—it’s just JavaScript.

Why Does console.log() Return undefined?

1. console.log() Prints but Doesn't Return Anything

In JavaScript, console.log() outputs to the console but has no return value—so it returns undefined by default.

Example:

const result = console.log("Hello");
console.log(result); // undefined
Enter fullscreen mode Exit fullscreen mode

Here, "Hello" gets printed, but console.log() itself doesn’t return anything useful—hence, result is undefined.

2. The Browser Console Reveals the Truth

When you type console.log("Hello") directly in the browser console, it prints "Hello" and then shows undefined as the return value:

Hello
undefined
Enter fullscreen mode Exit fullscreen mode

The undefined isn’t part of console.log(). It’s just the console displaying what the function returned.

3. The "Return Trap" in Functions

If you call console.log() inside a function and log its output, you'll see the same behavior:

function sayHello() {
  console.log("Hello");
}

console.log(sayHello()); 
// Logs: Hello
// Then: undefined
Enter fullscreen mode Exit fullscreen mode

Since sayHello() doesn't return anything, console.log(sayHello()) first prints "Hello" and then logs undefined.

4. When Things Get Weird: Overriding console.log()

For the truly paranoid, here’s how JavaScript can gaslight you:

console.log = () => undefined;
console.log("Hello"); // Nothing prints!
Enter fullscreen mode Exit fullscreen mode

If console.log() is redefined (by accident or an evil teammate), it won’t even log anything. Now that’s a true nightmare!

Final Thoughts

JavaScript didn’t lie—it just has some quirks that make you question your sanity. console.log() is a tool for debugging, not for returning values. So next time you see undefined, just remember: it’s not a bug, it’s a feature.

. . . . . . . . .