The JavaScript Void function does not return any value. Javascript void function is mostly used while putting an expression into a web application page that may produce an unwanted effect in the application.
<a href="JavaScript:Void(0);">Click here</a>
This is one the very common use of void function of JavaScript.
JavaScript Void function
Simple example code.
<!DOCTYPE html>
<html>
<body>
<div id="app"></div>
<a href="javascript:void(0)" >login</a>
</body>
</html>
Output:
Void in single expression arrow functions
The void operator evaluates the given expression and then returns undefined. When you don’t explicitly return
anything from a function, undefined
is returned by default. Using void
is just more characters and more obscure.
For example, if you have
void someFunction();
function doSomething(number) { return number + 1 }
const fn0 = () => doSomething(1)
const fn1 = () => { doSomething(1) }
const fn2 = () => void doSomething(1)
console.log(fn0()) // 2
console.log(fn1()) // undefined
console.log(fn2()) // undefined
Do comment if you have any doubts or suggestions on this JS void topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version