Alexandria Lalli
2 min readDec 7, 2023

--

I learned something today about javascript that I think is super cool and I wanted to share it.

I was mid-code-review at my job, when I saw a file that had a huge function. At the very end of it, there was a conditional return.
Something about this conditional return stood out to me. It wasn’t that it was conditional, but it was because of what it returned. Let me set the scene for you…..

const superCoolFunction = ({ itemOne, itemTwo, optionalItem = 'hi' }) => {
// lots of code in here

if (optionalItem !== 'hi'){
return undefined;
}

console.log({ itemOne, itemTwo });
return undefined;
};

Now…. why are we returning undefined, when we could just do: return;? This is the ONLY file in our entire arsenal that is set up this way. This was something that slipped through the cracks two years ago, and obviously it needs to be changed, but I also wanted to know why it needed to be changed, why this wasn’t industry standard, and why we don’t use this often.

So I looked it up! It turns out my intuition was correct. I would argue that industry standard should be return;, not return undefined;.

Ookay, but why, right? Both of these scenarios return the same thing.

const a = () => {
return; // returns "undefined"
};

const b = () => {
return undefined; // returns "undefined"
};

--

--

Alexandria Lalli

Full-Stack Engineer, UX/UI Aficionado, and lover of coffee.