What Does 'Void 0' Mean

What does javascript:void(0) mean?

The void operator evaluates the given
expression and then returns undefined.

The void operator is often used merely
to obtain the undefined primitive
value, usually using “void(0)” (which
is equivalent to “void 0”). In these
cases, the global variable undefined
can be used instead (assuming it has
not been assigned to a non-default
value).

An explanation is provided here: void operator.

The reason you’d want to do this with the href of a link is that normally, a javascript: URL will redirect the browser to a plain text version of the result of evaluating that JavaScript. But if the result is undefined, then the browser stays on the same page. void(0) is just a short and simple script that evaluates to undefined.

What does `void 0` mean?

What does void 0 mean?

void[MDN] is a prefix keyword that takes one argument and always returns undefined.

Examples

void 0
void (0)
void "hello"
void (new Date())
//all will return undefined

What's the point of that?

It seems pretty useless, doesn't it? If it always returns undefined, what's wrong with just using undefined itself?

In a perfect world we would be able to safely just use undefined: it's much simpler and easier to understand than void 0. But in case you've never noticed before, this isn't a perfect world, especially when it comes to Javascript.

The problem with using undefined was that undefined is not a reserved word (it is actually a property of the global object [wtfjs]). That is, undefined is a permissible variable name, so you could assign a new value to it at your own caprice.

alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"

Note: This is no longer a problem in any environment that supports ECMAScript 5 or newer (i.e. in practice everywhere but IE 8), which defines the undefined property of the global object as read-only (so it is only possible to shadow the variable in your own local scope). However, this information is still useful for backwards-compatibility purposes.

alert(window.hasOwnProperty('undefined')); // alerts "true"
alert(window.undefined); // alerts "undefined"
alert(undefined === window.undefined); // alerts "true"
var undefined = "new value";
alert(undefined); // alerts "new value"
alert(undefined === window.undefined); // alerts "false"

void, on the other hand, cannot be overidden. void 0 will always return undefined. undefined, on the other hand, can be whatever Mr. Javascript decides he wants it to be.

Why void 0, specifically?

Why should we use void 0? What's so special about 0? Couldn't we just as easily use 1, or 42, or 1000000 or "Hello, world!"?

And the answer is, yes, we could, and it would work just as well. The only benefit of passing in 0 instead of some other argument is that 0 is short and idiomatic.

Why is this still relevant?

Although undefined can generally be trusted in modern JavaScript environments, there is one trivial advantage of void 0: it's shorter. The difference is not enough to worry about when writing code but it can add up enough over large code bases that most code minifiers replace undefined with void 0 to reduce the number of bytes sent to the browser.

JavaScript: what does void 0 mean?

The void operator always evaluates as the undefined value.

The undefined variable, which defaults to holding the undefined value, can be overwritten.

What is void 0?

The void operator is often used merely to obtain the undefined primitive value,
usually using “void(0)” (which is equivalent to “void 0”). In these cases, the
global variable undefined can be used instead (assuming it has not been assigned
to a non-default value).

in this cases, the global variable undefined can be used instead:
ie:

_this[i] !== undefined;

Jsfiddle Demo

Should I use `void 0` or `undefined` in JavaScript

If you are using a modern browser, (which supports JavaScript 1.8.5) using undefined and void 0 would most likely yield the same result (since undefined is made not writable), except that the void can accept an expression as parameter and evaluate it.

In older browsers, (which do not support JavaScript 1.8.5) it is better to use void 0. Look at this example:

console.log(undefined);
var undefined = 1;
console.log(undefined);

It will print

1

undefined is actually a global property - it's not a keyword. So, undefined can be changed, where as void is an operator, which cannot be overridden in JavaScript and always returns the value undefined. Just check this answer which I gave earlier today for a similar question, Why does void in Javascript require an argument?.

Conclusion:

So, if you are concerned about compatibility, it is better to go with void 0.

Why use javascript:void(0) instead of # in href?

<a href="#">link</a>

adds # to the browser url and jumps to the top of the page.

<a href="javascript:void(0);">link</a>

simply "ignores" the link click.

<a href="#" onclick="return false;">link</a>

also ignores the href.

Don't forget that in some cases javascript might be disabled (very uncommon).

JavaScript: understanding void 0 in ?? operator polyfill

void 0 is the same thing as undefined, most of the time. There are two benefits to using void 0:

  • It's shorter (which can be useful for minifiers)

  • The identifier undefined can be created when not on the top level, leading to unexpected behavior:

(() => {
const undefined = 'foo';

// later in the code:

const actuallyUndef = (() => {})();
console.log(actuallyUndef === undefined);
})();

What's the effect of adding void(0) for href and 'return false' on click event listener of anchor tag?

About void(0):

As defined by @rahul in What does "javascript:void(0)" mean?

The void operator evaluates the given expression and then returns undefined.

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

"The reason you’d want to do this with the href of a link is that normally, a javascript: URL will redirect the browser to a plain text version of the result of evaluating that JavaScript. But if the result is undefined, then the browser stays on the same page. void(0) is just the smallest script possible that evaluates as undefined."

The return false:

Acts like a event.preventDefault negating it.

If you call a function like:

<button type="submit" onclick="return some_function();"></button>

And the some_function has a return false; the submit will not happen if you call it.. But a return true will continue with the submit when it's called.

In your case, you'll not be redirected when you click the link.

Why use void(0)?

Why people use void(x) instead of undefined?

Well both would work but undefined is a reserved variable and its value can be changed:

undefined = true;

This will give true instead of undefined.

Where as void() is a keyword which always returns undefined. Whatever you place inside the keyword:

void('return false plox'); //will return false

More info on this topic here: What does `void 0` mean?

jsFiddle


Note that <a href="#"> is not the same as it still acts as a link and will redirect you, where as the previous methods will cancel the event(similar to event.preventDefault).

Update

Since ECMAScript 5, the global undefined variable is no longer directly editable (See for example Mozilla docs). It now simply shadows the global variable as some have noted.



Related Topics



Leave a reply



Submit