undefined vs void
Written byPhuoc Nguyen
Category
JavaScript
Created
16 Jul, 2020
`void`
is an operator that evaluates a given expression and then returns `undefined`
.#Difference
In the modern browsers which supports ES5, there's no difference between using the
`void`
operator and the `undefined`
value directly:void 0 === undefined; // true
void 1 === undefined; // true
void 'Foo' === undefined; // true
However, in the old browsers which run ES3 engine,
`undefined`
is a global property and can be changed.// In ES3
console.log(undefined);
var undefined = 'foo';
console.log(undefined); // 'foo'
On the other hand, it's not possible to override the
`void`
operator. Hence, `void`
is used as a replacement for `undefined`
to get the `undefined`
value in a safe way.#Good to know
-
`void`
is an operator, not a function. So we don't need to wrap the expression in parentheses.`void 0`
is equivalent to`void(0)`
. -
There are minifiers which use
`void 0`
to shorten`undefined`
. -
If you use immediately-invoked function expression (known as IIFE),
`void`
can be used to treat the`function`
keyword as an expression, not a declaration.Assume that we want to execute the following IIFE function:function run() {console.log('Executed');}();then we can either use`void`
void (function run() {console.log('Executed');})();or wrap the function in parentheses as below:(function run() {console.log('Executed');})(); -
`void`
can be used to avoid the side effect when using with arrow functions.As we know, ES6 arrow functions allow to use the return value of function by omitting the braces from the function body.const sum = (a, b) => a + b;In some cases, we don't intend to use the return value of function, because it can lead to a different behavior. Let's say that we are going to handle the`click`
event of a button:button.onclick = () => doSomething();It works as usual until someone changes`doSomething`
, and makes it return a`boolean`
value. In the case it returns`false`
, the default behavior of the`click`
event will be skipped, and it maybe the thing you don't want to.Passing the result to`void`
will ensure that it doesn't change the behavior of arrow function regardless the result of executed functions:button.onclick = () => void doSomething(); -
The advantage of using
`void`
with arrow functions can be seen in modern libraries such as React, Svelte.These libraries allow us to execute a function right after a component mounted to the DOM. For example, React provides`useEffect`
, Svelte has`onMount`
.If we return a function inside the callback, then the function will be invoked to clean up things, free the memory before the component is removed from the screen.// React example codeuseEffect(() => doSomething());It can produce bugs at run time. To avoid that, we can either use`void`
useEffect(() => void doSomething());or using the braces:useEffect(() => {doSomething();});
#Good practice
The
`void`
operator has been used in the URL prefixed with `javascript:`
.By default, the browser will evaluate the code when following a
`javascript:`
URI, and then replaces the contents of page with the returned value.To prevent the default behavior, the code must return
`undefined`
. That's why we have been seeing the following code:<a href="javascript: void(0);" onclick="doSomething">
...
</a>
Nowadays, using
`javascript:`
protocol isn't recommended. It can create a security issue since user can put unsanitized input in the event handler:<a href="javascript: alert('unsanitized input')">...</a>
Starting from v16.9.0, React also deprecates the usage of
`javascript:`
URLs.#See also
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn