CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - void Keyword

JavaScript - void Keyword

Thevoidkeyword in JavaScript is used as an operator that evaluates a givenexpressionand returnsundefined. Thevoidis an important keyword in JavaScript. The meaning of the void isnullorempty.

Thevoidkeyword can be used as a unary operator that appears before its single operand, which may be of any type. This operator specifies an expression to be evaluated without returning a value or returning the undefined.

Syntax

The syntax to use the void keyword in JavaScript βˆ’

JAVASCRIPT CODE
void operand;

In the above syntax, the operand can be any expression.

Return Value

It returns the undefined.

Example

In the below code, we used the 10 with the β€˜void’ keyword. In the output, you can observe that it returns undefined.

Preview

Output

JAVASCRIPT CODE
The value of the ans variable is: undefined

Importance of Precedence of void Keyword

Generally, the JavaScript β€˜void’ keyword is used to return the primitive undefined value, but if you don’t take precedence in the consideration, it can return a different value.

Let’s understand it via the example below.

Example

In the below code, the void operator takes precedence over the strict equality operator in the first expression. So, it first evaluates β€˜void 10’ to undefined and compares it with 20. So, it returns false.

The second expression evaluates β€˜10 === 20’ first to false and evaluates β€˜void false’ to undefined.

Preview

Output

JAVASCRIPT CODE
false
undefined

What is javascript:void(0)?

Let’s break down the javascript:void(0) into two parts and understand both separately.

javascript:

You can use the β€˜javascript:’ to create a pseudo URL. Using β€˜javascript:’ you can create the interactive URL.

You need to write the expression after β€˜javascript:’, and when users click the anchor text, it executes the JavaScript code.

Let’s understand it via the example below.

Example

In the below code, we have created the link text and used the JavaScript URL as a href. When you click the anchor text, it writes it in the HTML document.

Preview

In this way, you can use the β€˜javascript:uri’ to create the pseudo URL.

void(0)

The void(0) evaluates the JavaScript expression, but it returns undefined. So, when you want to execute the expression without performing any action, you can use the void(0).

javascript: void(0)

When you don’t want to navigate users to another web page when they click on the link, you can use the β€˜javascript:void(0)’ as a href value of the anchor tag.

Let’s understand it via the example below.

Example

It won’t do anything Whenever you click the anchor text in the code below.

Preview

You can also use the β€˜javascript:void(0)’ when you want to update the web page using the URI but don’t want to navigate to another web page.

Example

In the below code, when you click the anchor text, it will change the background color of the body, rather than navigating to another web page.

Preview

The void Keyword with Functions

When you use the void keyword with JavaScript functions, it returns undefined. After that, if you try to execute the function, it will throw an error, as the β€˜void’ operator considers the function as its operand and evaluates it as undefined.

Example

In the below code, we have defined the test() function and used the β€˜void’ keyword with it.

Also, used the trycatch block to catch the error.

In the try block, we execute the function, and in the output, you can observe that control goes into the catch block.

Preview

Output

JAVASCRIPT CODE
The test function is undefined!

The void Keyword with Immediately Invoked Function

When you use the β€˜void’ keyword with the JavaScript immediately invoked function, it invokes the function expression first and evaluates it as undefined.

Example

In the below code, we have defined the immediately invoked function with the void keyword. You can observe that it invokes the function first and returns undefined.

Preview

Output

JAVASCRIPT CODE
Unknown function is executed!

Also, you can use the β€˜void’ keyword with the arrow function to return undefined from the arrow function. The JavaScript:void() URI can also be used to prevent the web page reloading by default, like the preventDefault() method.