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 β
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.
Output
The value of the ans variable is: undefinedImportance 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.
Output
false
undefinedWhat 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.
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.
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.
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.
Output
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.
Output
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.