What Does Void 0 Do In JavaScript? - Mastering JS
Maybe your like
The void operator in JavaScript evaluates an expression and returns undefined. At first glance, this operator doesn't seem useful, but there are 3 cases where you may see the void operator in practice. Here are the 3 major use cases:
No Overwriting undefined
Surprisingly enough, undefined is a valid variable name in JavaScript. The below script will print out "test" twice.
const undefined = 'test'; function foo() { return undefined; } function bar(undefined) { return undefined; } console.log(foo()); console.log(bar('test'));However, void is an operator. That means, if you try to name a variable void, JavaScript will throw the below error.
SyntaxError: Unexpected token voidSome JavaScript projects prefer void 0 instead of undefined to avoid accidentally creating variables named undefined. There is even an ESLint rule to disallow undefined for this reason.
No-Op for <a> Tags
Another use case for the void operator is to make an <a> tag with a javascript: URI do nothing. An <a> tag is not valid HTML unless it has a name or href property, so javascript: void(0) is a common trick to make an <a> tag render correctly without adding an actual link.
<a href="javascript:void(0)">Click Here</a>Below is how the above HTML looks:
Click Here
Immediately Invoked Function Expressions (IIFEs)
To declare a function and execute it in the same statement, you normally would use parentheses to force JavaScript to handle the function declaration as an expression.
(function() { console.log('Hello, World'); })(); // Prints "Hello, World"You may occasionally see the void operator as an alternative to parentheses. Because void is a unary operator, it tells JavaScript to treat the function declaration as an expression, so the below code prints "Hello, World."
void function() { console.log('Hello, World'); }();The difference is that a normal IIFE can still return a value, whereas using void for IIFE will always evaluate to undefined.
(function() { return 42; })(); // 42 void function() { return 42; }(); // undefined Did you find this tutorial useful? Say thanks by starring our repo on GitHub!More Fundamentals Tutorials
- The `setTimeout()` Function in JavaScript
- JavaScript Array flatMap()
- How to Get Distinct Values in a JavaScript Array
- Check if a Date is Valid in JavaScript
- Encode base64 in JavaScript
- Check if URL Contains a String
- JavaScript Add Month to Date
Tag » What Does Do In Javascript
-
JavaScript Operators Reference - W3Schools
-
Expressions And Operators - JavaScript - MDN Web Docs
-
Bitwise Operators - What Does '!!~' Do In Javascript? - Stack Overflow
-
What Do The Three Dots (...) Mean In JavaScript? - Adrian Oprea
-
The JavaScript `in` Operator Explained With Examples
-
How The Question Mark (?) Operator Works In JavaScript
-
JavaScript Operators - GeeksforGeeks
-
Javascript Operators (With Examples) - TutorialsTeacher
-
What Does A Modulus Operator Do In JavaScript? - Linux Hint
-
What Does The Operator || Do In A Var Statement In JavaScript?
-
What Does ! --> Mean In JavaScript? - Quora
-
What Does This ${} Syntax Do? - JavaScript FAQ - Codecademy Forums
-
What Does The Dot Do In JavaScript? - Jim Fisher
-
What Is JavaScript? What Does It Do, And What Is It Used For?