This page looks best with JavaScript enabled

Function returns undefined unless specified otherwise

 ·   ·  ☕ 1 min read

Why is my function returning undefined when I have not mentioned that anywhere.

The crux of the issue is in the topic header.

All functions have to return something. If nothing is specified, they return undefined.

1
2
3
4
5
6
7
function doSomething() {
  // do something
}

const something = doSomething();
console.log("something: ", something);
// undefined

You may however chose to ignore receiving something from the function.

1
2
3
4
5
function doSomething() {
  // do something
}

doSomething();

Typically, I like to return at least a true or false - just for the heck of it. That’s overengineering to some, but that’s ok.

1
2
3
4
function doSomething() {
  // do something
  return true;
}
Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things