blog

Home / DeveloperSection / Blogs / Java Script-Void Keyword

Java Script-Void Keyword

Danish Khan 3813 09-Nov-2012
Java Script-Void Keyword

Introduction :

In this blog I am going to explain the use of Java Script Void Keyword, what is Void Keyword and how to use it.

When working with Java Script if we click on a link the Browser loads a new page or it refreshes that same page, but if we do not want browser to load page or refresh it we can use Java script void() Keyword and pass a parameter 0 (Zero) to it. When using void keyword then we can dynamically update fields of our forms without the page load or refreshing the page. This operator defines an expression which will be evaluated without returning any value. It is commonly used for client side JavaScript where it allows us to evaluate expressions of side effects without the browser displaying the value of the estimated expression.

Its syntax is:
<head runat="server">
    <script type="text/javascript">
       void (func())
        javascript:void(func())
    </script>
</head>

Sometimes when we are working with hyperlinks, as soon as user click on the hyperlinks, it leads to a new page, but there are conditions when we have to perform a useful action like updating the sum on the same page without loading the new page, it is here that we get the advantage of JavaScript Void-Keyword.

                Void is Keyword not a function, it takes an expression of any type as its operand and returns undefined. The void(0) function actually returns null and works wonderfully by cancelling the page load.

Example of using JavaScript void keyword:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <a href="javascript:void(alert('You ckicked on this hyperlink!'))">Click Here!</a>;
    </div>
    </form>
</body>
</html>

Another use of JavaScript void keyword with example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="javascript:void(0)">Click Here!</a>
    </div>
   </form>
</body>
</html>

It will do nothing because we have pass 0 as a parameter.

Another use of JavaScript void Keyword to generate the undefined value with example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        function generateVal() {
            var num1, num2, num3;
            num1 = void (num2 = 15, num3 = 45);
            document.write('num1 value = ' + num1 + ' , num2 value = ' + num2 + ' , num3  
            value= ' + num3);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="btnFindVal" type="button" value="Find Value" onclick="generateVal()" />
    </div>
    </form>
</body>
</html>

Output:

num1 value=undefined, num2 value= 15, num3 value=45

Conclusion:

Through this blog we came to know how to use void keyword in javascript which is mostly used in some places when we want to cancel the page load.

 


Updated 18-Sep-2014

Leave Comment

Comments

Liked By