Explain Empty Returns Like I'm Five

This kind of code can be confusing for beginners when they start coding:

function doSomething() {
    if (somethingElseHappens())

    // ...

How would you explain this in a real life scenario that could tell how/why this is useful?

Did you find this post useful? Show some love!

You decided to go shopping for a gift(doSomething).
As you arrived at a train station, you got a push notification that all trains are canceled due to hurricane (somethingElseHappens()).
So you returned home without any gifts (return).
Or else you could've waited for hours for a train that will never show up.

That was a super useful push notification! ✨

I should've used FEMA Alert as an example πŸ˜„

Woah, I listen to your podcast all the time - never thought I'd see your name pop up in the wild!

Its commonly useful for when you need to end the function rather than return anything from the function.

In other words: functions don't always need to return a value to be useful, that can also perform tasks like printing a bunch of "Hello Worlds" to the screen. Whenever we return in a function, it stops executing that function (unless called again) at that exact line. It doesn't need a value to do this (technically it does, as blank is void & void is a value).

In short: the return statement isn't so much a "return" statement, as it is a statement that ends the function, and "returns" a value if there is one associated with it.

Hey RedSpy,

Thanks for taking your time to comment. But the #explainlikeimfive hashtag is intended to be sort of like a "challenge" that involves explaining concepts with day to day situations so that an actual 5 year old can understand.

This may make things harder for those who reply but I've seen some really creative ideas here and there.

You can take these as example from Explain Dependency Injection Like I'm Five

Traditional: Picking what to wear when you get up in the morning.

Dependency Injection: Asking a pants, t-shirt and an hat from a stylist, and he makes sure you look lit AF.

"Dependency Injection" is a fancy way of saying that you have to ask someone else, like Mummy or Daddy, to give you anything you want to play with. You can't make the toy yourself, you need Mummy and Daddy to get it for you.

Sometimes, you don't know what toy you are going to get, like on your birthday, but all you need to know is that it will be a fun toy you can play with when you open the present.

Yah, I thought about that afterwards. If you would like me to remove the reply, I will

I did make sure it was simple enough that at least a 6 year old could understand (had to make this pun, sorry)

Hahaha, don't worry πŸ˜‚. Feel free to add the new one.

If I may add my two cents: all kinds of explanations are welcome on #explainlikeimfive. If we take the hashtag literally, we can't really get to the heart of a lot of technical topics. I mean, when I was five I was still potty training (sad, I know). I think simple--but perhaps not five-year-old simple--explanations are super valuable and this is a great place for them.

Hey Isaac,

Yeah, you are right. I apologize if the way I expressed myself sounded like it was a mandatory way of doing it.

Its like when you go to the kitchen for cookies but there are no cookies so you return with nothing

Some functions are designed to return a value while others have a job to do that doesn't require anything to be returned. Return means exit the function either way but, if the design of the function expects a value to be returned, it must follow return like return 23;

When you start coding, you usually put returns at the end of functions after the work has been done. But after a while returns are placed inside ifs and loops and you usually end up coding multiple returns depending on the state of the application.

It's really nice in validation functions, where certain conditions must be met before proceeding. By returning early (maybe with a log about why), you keep the validation logic entirely flat. The alternative is making a deeply nested set of checks that are even more difficult to understand, since deep nesting makes it more difficult to know which lines of code are related

I'd add more to the function:

function doSomething() {
    if (somethingElseHappens())



function doCookieCheck() {


In addition to the abstract view of this situation, it's probably worth noting that different languages have different technical behaviors when it comes to returning nothing.

For example, Python will have the function call expression evaluate to "None", which is more-or-less its null value.

JS has it evaluate to "undefined".

Java code won't compile if it's possible for a function with a defined return type (!= void) to return nothing.

Classic DEV Post from Oct 5

How do you do random?

How would you generate an random 0 to 10 integer in your favorit language?

Christian Vasquez
Join dev.to