Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is purpose of using `void` here? [duplicate]

Tags:

javascript

Possible Duplicate:
what is the point of void in javascript

What is purpose of using void here ? if just remove void(), it should also work, right?

var b=document.body;

if(b&&!document.xmlVersion) {
   void(z=document.createElement('script'));
   void(z.src='http://www.google.ca/reader/ui/subscribe-bookmarklet.js');
   void(b.appendChild(z));
}
else {
  location='http://www.google.com/reader/view/feed/'+encodeURIComponent(location.href);
}
like image 402
nandin Avatar asked May 25 '11 19:05

nandin


People also ask

What is the point of using void?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.

What is the significance of void * in C?

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.

What is one reason you would use a void pointer?

Why do we use a void pointer in C programs? We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type.

Why do we use void as it is returning nothing?

The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.


3 Answers

void is a keyword that runs an expression and returns undefined

void 0 === undefined

void (foo = 42) === undefined

As to how href="javascript:..." work.

unless the returned value is undefined.

You need to return undefined or else the page is overwritten. Using the void keyword is the easiest way to achieve this.

like image 181
Raynos Avatar answered Oct 21 '22 04:10

Raynos


Yes. it will work without void.

it isn't necessary to use void there.

like image 43
Falcon Avatar answered Oct 21 '22 04:10

Falcon


I googled your code snippet and it looks like its typically embedded in a link with "javascript:" in front of it. To quote the Mozilla reference for the void operator:

JavaScript URIs

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. For example:

<a href="javascript:void(0);">Click here to do nothing</a>
<a href="javascript:void(document.body.style.backgroundColor='green');">Click here for green background</a>

Note, however, that javascript: URIs are now often discouraged over other alternatives, such as events.

source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void

So it keeps the contents of the page from being overwritten when the code is executed inside of a link.

In this case, if the code is executed without the javascript: URI, the void operator should not make any difference. The void operator simply evaluates its input expression and returns undefined.

like image 36
patorjk Avatar answered Oct 21 '22 04:10

patorjk