When does a function returns "undefined"?

When does a function returns "undefined"?

Functions are a huge part of almost every programming language. They are used to perform a specific task or calculation and return the value. Functions help in simplifying and organizing the code by breaking down the complex tasks into smaller, reusable units.

For eg., a function formatting the date.

function getRandomNumber(min, max) {
    const randomNum = Math.floor(Math.random() * (max - min + 
1) + min);
    return randomNum;
}

The above function generates a random number between two numbers and returns the value. So, whenever, in my code I need to generate a random number, I can call the getRandomNumber() function. But why do functions return a value?

Functions return a value

Functions often return a value which is useful in following ways:

  • Functions provide the result. They are used to perform a task and return a result which can be used somewhere else in the code or the returned value can be used to further manipulate the data.

    For eg., the above function can be further used like this:

      const rollTheDice = `The Dice was rolled and You got ${getRandomNumber(1, 6)}.`;
      console.log(rollTheDice); //The Dice was rolled and You got 3.
    
  • Functions can be called multiple times which means that the returned value can be used at multiple places, at multiple ways by just calling the function where we need them.

  • By returning values, functions can be combined and composed in new ways.

Why do functions return undefined?

Sometimes, functions return undefined. There can be several reasons for a function to return undefined.

  1. If a function does not have a return value i.e. It does contain a return statement, the function will return undefined by default.

     function noReturnStatement() {
         const randomString = "randomString";
     }
     noReturnStatement(); //undefined
    
  2. If a function has a return statement, but there is no value specified, it will return undefined .

     function returnButNoValue() {
         return;
     }
     returnButNoValue(); //undefined
    
  3. If a function specifically returns an undefined value.

     function returnUndefined() {
         return undefined;
     }
     returnUndefined(); //undefined
    
  4. If a function returns a variable that has not been declared.

     function getVariableWithNoVariable() {
         const iHaveNoValue;
         return iHaveNoValue;
     }
     getVariableWithNoVariable(); //undefined
    

Why does console returns "undefined" when logging?

You might or might not have noticed while debugging that the console.log() outputs its argument to the console but It also returns undefined .
why is that?

The same reason mentioned above. console.log() is a JavaScript function. It logs the value passed to it to the console, but It does not return a value. Therefore, It returns undefined .

The purpose of console.log() is mainly to show the information in the console for debugging purposes and not to return a value for further use in the code.

Conclusion

  • Functions are used to simplify and organize the code by breaking down the problems into small, reusable units.

  • Functions return value for further use in the code.

  • Functions return undefined for several reasons.

  • console.log() does not return a value. Hence, It returns undefined in the browser console.