Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptES6 Void Keyword

ES6 Void Keyword

The void keyword in JavaScript ES6 is used to evaluate an expression and it does not return any value. It is a unary operator and takes a single operand. We commonly use it in hyperlinks. Sometimes, we may need to call some JavaScript from a link. When we click on a link, the browser loads a new page or refreshes the same page. But we don’t want that to happen if some JavaScript is attached to that link and the void operator is useful when we have to call another function, which might result in a page refresh. 

Syntax: 

void (expression)
void expression

Below examples illustrate the void keyword in ES6 JavaScript: 

Example 1:  

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>ES6 void keyword Example</title>
        <style>
            body{
               text-align: center; 
            }
            h1 { color: green;
            }
        </style>
    </head>
    <body>
       
        <h1>neveropen</h1>
        <b>A Computer Science Portal for Geeks</b>
        <br><br>
        <a href="javascript:void(func());">
         Click me to activate alert
        </a>
        <br><br>
        <a href=
"javascript:void(document.body.style.backgroundColor='#F08080');">
         Click me to change the background
        </a>
            <script>
            var func = function() {
                alert("HI!!!");
            };
        </script>
    </body>
   
</html>


Output: 

Example 2:

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>ES6 void keyword Example</title>
        <style>
            body{
               text-align: center; 
            }
            h1 { color: green;
            }
        </style>
    </head>
    <body>
       
        <h1>neveropen</h1>
        <b>A Computer Science Portal for Geeks</b>
        <br><br>
        <a href
"javascript:void(javascript:alert('A Computer Science Portal'))"
         Click here for no alert 
        </a
        <br><br>
        <a href
"javascript:alert('neveropen');">
            Click here for an alert
        </a>
    </body>
</html>


Output: 

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments