DEV Community

Ishan Bagchi for Byte-Sized News

Posted on

2 1

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

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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
pengeszikra profile image
Peter Vivo

Technically console.log just a side effect of the browser. Maybe implemented a different way of a different browser or JS runtime environment.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

...this is all very reasonable behaviour though, and JS is far from the only language that does this.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay