The Wayback Machine - https://web.archive.org/web/20240809213524/https://www.geeksforgeeks.org/es6-void-keyword/
Open In App

ES6 Void Keyword

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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>GeeksforGeeks</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>GeeksforGeeks</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('GeeksforGeeks');">
            Click here for an alert
        </a>
    </body>
</html>


Output: 

 



Similar Reads

What is void and when to use void type in JavaScript ?
In this article, we will know what is void operator in Javascript, along with discussing the particular condition where void operator can be used &amp; understanding their implementation through the examples. The void keyword in JavaScript, is used to evaluate an expression which does not return any value. The void operator is an unary operator tha
5 min read
Describe the void elements in HTML
Most of the HTML elements are surrounded by start and end tags to specify the starting and end of the element. There is a special group of elements that only have start tags and does not contain any content within it, these elements are called void elements. Void elements doesn't have ending tags and can only have attributes but do not contain any
3 min read
Which href value should we use for JavaScript links, "#" or "javascript:void(0)" ?
In this article, we will know which "href" should be used for JavaScript when we can use "#" or "javascript:void(0)". Let us understand it. Using the '#' value: The # is a fragment identifier used to refer to a specific section of the same document. When a user clicks a link with a # href, the browser will scroll to that section of the page specifi
3 min read
What is the use of the Void Operator in JavaScript ?
The void operator in JavaScript is like a special command that tells the computer to do something but not worry about what comes back. It's often used when you want to make a clickable link on a website that doesn't take you to a new page. Instead of saying "go nowhere," you use 'void(0)' in the link, and it makes sure nothing happens, keeping thin
1 min read
What does javascript:void(0) mean?
In JavaScript, javascript:void(0) is a pseudo-URL that does nothing when clicked. It's commonly used as a placeholder in href attributes to prevent the browser from navigating to a new page while still executing JavaScript code. Using "#" in an anchor tagWhen writing the following code in the editor, the web page is refreshed after the alert messag
1 min read
TypeScript Return type void
The void type in TypeScript represents the absence of a return value for functions. It indicates that a function does not return any value, ensuring that the function's purpose is solely to act without returning data. Syntaxfunction functionName(parameters: ParameterType): void { // Function body // No return statement or return type annotation is
3 min read
TypeScript void Function
Void functions in TypeScript are functions that do not return a value. They perform actions or computations without producing a result that needs to be captured. Commonly used for side effects like logging, modifying external state, or triggering asynchronous operations, they enhance code clarity. Syntax: function functionName(parameters: Parameter
3 min read
ES6 RegEx
A RegEx is the short form of the Regular Expression, which is the sequence of characters that define a pattern. The characters in a RegEx can be alphabets, numbers or special characters. Usually, RegEx is used to find a pattern in a string to replace, delete or modify them. In ES6, a RegEx can be defined by 2 different notations. Literal notation:
3 min read
Arrow operator in ES6 of JavaScript
ES6 has come with various advantages and one of them is the arrow operator. It has reduced the function defining code size so it is one of the trending questions asked in the interview. Let us have a deeper dive into the arrow operator's functioning. Syntax: In ES5 a function is defined by the following syntax: function functionName(arg1, arg2â€Ķ.) {
4 min read
ES6 Top features and syntax
ES6 or as it is officially called: ECMAScript2015 is a new JavaScript implementation and is arguably the hottest topic in the JS developer's conventions and meetups why it should not be: JavaScript rules the web and is gaining a foothold in every other field possible, be it robotics(nodebots), desktop applications(using ion framework), chatbots, et
3 min read
three90RightbarBannerImg