Last year I read the book "javascript- The Good Parts" by Douglas Crockford.
In this book Crockford considers void as the bad parts of Javascript. He mentions in his book that-
In this book Crockford considers void as the bad parts of Javascript. He mentions in his book that-
In many languages, void is a type that has no values. In JavaScript, void is an operator that takes an operand and returns undefined. This is not useful, and it is very confusing. Avoid void.
But if you've been doing web development, you would have probably seen links that look something like this:
<a href="javascript:void 0;" onclick="doSomething()">Do something</a>
What does an anchor tag with an href attribute having a value like href="JavaScript:void(0);",
actually mean?
To understand the meaning or purpose of such a hyperlink, we should first understand the void operator.
What does void 0 mean?
The void operator evaluates the given expression and then returns undefined.
actually mean?
To understand the meaning or purpose of such a hyperlink, we should first understand the void operator.
What does void 0 mean?
The void operator evaluates the given expression and then returns undefined.
void 0
void (0)
void "abc"
void (new Date())
//all will return undefined
void (0)
void "abc"
void (new Date())
//all will return undefined
The void operator is often used merely to obtain the undefined primitive value, usually using "void(0)" (which is equivalent to "void 0").
OK... got it...
But still it seems useless, isn't it!!!
I mean "What's the point of that?" More straightforward question is what's wrong with just using "undefined" itself?
Now to understand why people use void(x) instead of undefined, let us run the below code in IE8.
OK... got it...
But still it seems useless, isn't it!!!
I mean "What's the point of that?" More straightforward question is what's wrong with just using "undefined" itself?
Now to understand why people use void(x) instead of undefined, let us run the below code in IE8.
console.log(undefined); //outputs"undefined"
var undefined = "no longer undefined";
console.log(undefined) // outputs "no longer undefined"
var undefined = "no longer undefined";
console.log(undefined) // outputs "no longer undefined"
The problem with using undefined was that undefined is not a reserved word (it is actually a property of the global object). That is, undefined is a permissible variable name, so you could assign a new value to it.
However any environment that supports ECMAScript 5 or newer will have the undefined property of the global object as read-only, therefore this will not be a problem.
Now that we know what is the point of using void operator, let us move forward and understand the usability of this void with respect to the hyperlinks.
Sometimes, we may need to call some JavaScript from within a link. Normally, when we click a link, the browser loads a new page or refreshes the same page (depending on the URL specified). But we probably don't want this to happen if we've attached some JavaScript to that link.
When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined. If the result is undefined, then the browser stays on the same page.
To prevent the page from refreshing, we could use JavaScript:void(0).
However any environment that supports ECMAScript 5 or newer will have the undefined property of the global object as read-only, therefore this will not be a problem.
Now that we know what is the point of using void operator, let us move forward and understand the usability of this void with respect to the hyperlinks.
Sometimes, we may need to call some JavaScript from within a link. Normally, when we click a link, the browser loads a new page or refreshes the same page (depending on the URL specified). But we probably don't want this to happen if we've attached some JavaScript to that link.
When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined. If the result is undefined, then the browser stays on the same page.
To prevent the page from refreshing, we could use JavaScript:void(0).