The void "Function" in JavaScript
Jul 22, 2021
JavaScript's void
operator always returns undefined
, no matter what argument you pass it.
void 0
and void(0)
are equivalent:
void 0; // undefined
void(0); // undefined
void(0)
is often used as a href
attribute on an a
tag. The below syntax makes a
do nothing, preventing an unwanted page refresh:
<a href="javascript:void(0)">
Nothing Special Link
</a>
Although void(0)
looks like a function call, remember that void
is not a function.
void(void); // SyntaxError: Unexpected token ')'
More Fundamentals Tutorials
- Convert a Set to an Array in JavaScript
- What Does Setting the Length of a JavaScript Array Do?
- Get the Last Element in an Array in JavaScript
- Skip an Index in JavaScript Array map()
- Conditionally Add an Object to an Array in JavaScript
- Validate Emails using Regex in JavaScript
- JavaScript Copy to Clipboard