Void Keyword in ES6

Void Keyword in ES6

In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the undefined value. In JavaScript, the void keyword is also used as a unary operator, and it is shown before the single operand. The void operator is widely used to access the undefined primitive value.

Syntax

void expression

Example

We have an example of the void keyword.

var a,b,c;
a = void (b = 10, c = 20);
console.log (‘a = ’ + a + ‘b = ’ + b + ‘c = ’ + c );

Output

After the execution of the above code, we got the following output:

Void Keyword in ES6

Void keyword and JavaScript URI

The term URI can be defined as a JavaScript library that helps us to work with URLs. The JavaScript URI is often used as a syntax within an HTML page. If the browser peruses the URI, then it accesses the URI code. It also changes the page data with the returned value.

The void operator always returns the missing value.

Example

Here, we have an example to understand the above concept.

<html>  
<head>   
</head>  
<body>  
    <center>  
        <h2>Hello ES6</h2>  
        <h3>Welcome to ES6</h3>  
        <h2>Click on the links to see the changes</h2>  
        <a href = "javascript:void(javascript:alert('hello world!!'))">  
            It is only a sample link. 
          </a>  
          <br/><br/> 
          <a href = "javascript:alert('Welcome to ES6');">Click here for an alert</a> 
    </center>  
</body>  
</html>

Output

After the execution of above example, we got the following example:

Void Keyword in ES6

If we click on the first link, then we will get nothing. But if we click on the next link, then we will get the following alert message as an output.

Void Keyword in ES6

Void Keyword and Immediately Invoked Function Expression (IIFE)

The void keyword with IIFE refers to as an expression instead of the declaration.

Example

We have an example given below to understand the following concept.

void function welcome()
{
          var alert = function ()
{console.log(“Welcome to ES6”)};
alert();
}(); 

Output

We got the following output, after the execution of the above code.

Void Keyword in ES6