The “void” keyword in JavaScript is a unary operator that evaluates an expression and returns undefined. It is often used to prevent the default behavior of a link or a form submit button, which is to navigate to a new page.

The syntax for using “void” in JavaScript is as follows:

void expression;

Where “expression” is any valid JavaScript expression that you want to evaluate.

When the “void” keyword is used with an expression, it evaluates the expression and discards the result.

It then returns undefined, which can be useful in cases where you want to execute some JavaScript code but don’t want the browser to navigate to a new page.

The common uses of JavaScript void

JavaScript void is a keyword that is commonly used to prevent a page from being redirected to another URL.

When the user clicks on a hyperlink that is attached to an event, such as a button or a link, the browser will execute the JavaScript code associated with that event.

The purpose of using “void” in JavaScript is to prevent the browser from loading a new URL when the user clicks on a link or button.

Understanding how void works in JavaScript

To understand how “void” works in JavaScript, let’s take an example of a hyperlink that is attached to an event.

Suppose you have a hyperlink on your webpage, and you want to execute a JavaScript function when the user clicks on that link.

You can use the “void” keyword to prevent the browser from loading a new URL and to execute the JavaScript function.

Here’s an example of how to use “void” in JavaScript:

<a href="javascript:void(0);" onclick="myFunction()">Click me</a>

In the above example, we have used the “void” keyword to prevent the browser from loading a new URL when the user clicks on the link.

Instead, we have attached an “onclick” event to the hyperlink, which will execute the “myFunction()” JavaScript function.

You can also use the “void” keyword to prevent the browser from loading a new URL when the user submits a form.

Using JavaScript void in HTML form

Here’s an example of how to use “void” with a form submit button:

<form onsubmit="myFunction(); return false;">
  <input type="submit" value="Submit">
</form>

In the above example, we have attached an “onsubmit” event to the form, which will execute the “myFunction()” JavaScript function when the user submits the form.

We have also used “return false;” to prevent the browser from loading a new URL.

In conclusion, the “void” keyword is a useful tool in JavaScript to prevent the browser from loading a new URL when the user clicks on a link or submits a form.

By using “void,” you can execute JavaScript functions without disrupting the user’s browsing experience.

Evaluate an expression using void

In addition to preventing a page from being redirected to a new URL, “void” can also be used to evaluate expressions and return undefined.

Here’s an example of how to use “void” to evaluate an expression and return undefined:

void(2 + 2);

In the above example, we have used “void” to evaluate the expression “2 + 2,” which will return the value 4. ‘

However, since we have used “void,” the returned value will be undefined.