Skip to content

Void function return value is used in JavaScript

  • by

JavaScript Void function return value is used as the return type of function that does not return any value. The void operator is often used for obtaining the undefined primitive value.

void expression  

Void function return value is used in JavaScript

A simple example code describes the returning undefined value.

<!DOCTYPE html>
<html lang="en">
<body>
  
  <script>
   function foo() {
     return void 0;
   }
   console.log(foo());
 </script>

</body>
</html>

Output:

Void function return value is used in JavaScript

Using an IIFE, the void can be used for forcing the function keyword to be treated as an expression rather than a declaration.

void function hello() {   
       var msg = function ()   
       {console.log("Welcome back!!")};   
       msg();   
    }();// Welcome back!!

Returning Undefined Value by converting any variable’s value to an undefined type.

var x,y,z;    
x = void ( y = 50, z = 70 );  
console.log('x = ' + x + ' y = ' + y +' z = ' + z );  

Output: x = undefined y = 50 z = 70

Does every Javascript function have to return a value?

Answer: No, return is not necessary. When no return statement is specified, undefined is returned.

In JS, like in almost every language, you’re free to simply ignore the return value of a function, which is done an awful lot:

(function()
{
    console.log('this function in an IIFE will return undefined, but we don\'t care');
}()); 

//this expression evaluates to:
(undefined);//but we don't care

Do comment if you have any doubts or suggestions on this Js void function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading