w3resource

JavaScript: void operator

Description

The void operator is used to evaluate a JavaScript expression without returning a value.

Syntax

void (expression)
void expression

Example -1:

The following web document shows how the void operator is used.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript void operator example</title>
</head>
<body>
<a href="javascript:void(alert('Thank you.'))">Click here to see a message</a>
</body>
</html>

View the example in the browser

Example -2:

In the following web document, void operator is used to call a function.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript void operator (calling function) example</title>
</head>
<body>
<script type="text/javascript">
</script>
<a href="javascript:void(testfunc())">
Click here to call testfunc()</a>
</body>
</html>

View JavaScript void operator ( call a function) example in the browser

See also

Conditional Operator
comma
delete
function
in
instanceof
new
this
typeof

Previous: JavaScript: typeof operator
Next: JavaScript: Block statement

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Using the window.location object

JavaScript can access the current URL using the window.location object. Pretty neat, but even cooler is that this object contains certain parts of the URL as well.
Get access to the protocol/host/pathname/search/and more!

// JavaScript can access the current URL in parts. For this URL:
'https://thatsanegg.com/example/index.html?s=article'
window.location.protocol == 'https:'
window.location.host == 'thatsanegg.com'
window.location.pathname == '/example/index.html'
window.location.search == '?s=article'

Ref: https://bit.ly/2LBj213