---
title: "Why are use the void keyword in JS?"  
description: "Why are use the void keyword in JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93752/why-are-use-the-void-keyword-in-js  
category: "web application"  
tags: ["javascript", "web development", "web application development"]  
reading_time: 2 minutes  

---

# Why are use the void keyword in JS?

Why are use the void [keyword](https://www.mindstick.com/blog/389/java-script-void-keyword) in JS?

## Answers

### Answer by user

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 that might result in a page refresh. **Syntax:** void (expression) void expression **Example:**

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
 <head>
         <meta charset='utf-8' />
  <title>ES6 void keyword Example</title>
 </head>
 <body>
  <br><br>
  <a href='javascript:void(func());'>
  Click me to activate alert
  </a>
  <br><br>
  <a href='javascript:void(document.body.style.backgroundColor='yellow');'>
  Click me to change the background
  </a>
   <script>
       var func = function () {
           alert('Hello this is void keyword demo !!!!!');
       };
  </script>
 </body>
</html>
```

\

### Answer by Ethan Karla

The void is an important keyword in JavaScript. which can be used as an operator. which may be any type of data for access. It returns undefined values.

## Syntax

```
<head>   <script type = 'text/javascript'>         void func(){             javascript:void func()         } // or:         void(func())             javascript:void(func())   </script></head>
```

The most common use of void is in client-side JavaScript URLs. we can not change the page after clicking the link then we can add these void keywords on the click event.

```
<html>
   <head>
      <script type = 'text/javascript'>

      </script>
   </head>
   <body>
      <p>Click the following, This won't react at all...</p>
      <a href = 'javascript:void(alert('Warning !!!'))'>Click me!</a>
   </body>
</html>
```

```
<html>
   <head>
      <script type = 'text/javascript'>
      </script>
   </head>
   <body>

      <a href = 'javascript:void(0)'>Click me !!!</a>
   </body>
</html>
```


---

Original Source: https://answers.mindstick.com/qa/93752/why-are-use-the-void-keyword-in-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
