3

After reading both :

difference between "void 0 " and "undefined" , https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

I still have some questions.

I've read that window.undefined can be overwritten vwhere void operator will return the undefined value always

But the example which caught my eyes was the one in MDN :

<a href="javascript:void(0);">Click here to do nothing</a>

In order to do nothing , I always thought I should write :

href="javascript:return false;"

And this leads me to another question : (at Href context !) :

javascript:void(0); vs javascript:return false;

What is the differences ?

Also - Does

function doWork() {
    return void( 0 );
}

is exactly

function doWork() {

    return undefined;
}

Thanks.

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • Your writing is all over the place, and your "please 2 questions" don't help to clarify things either... – BoltClock Sep 27 '12 at 13:56
  • @BoltClock Sorry, should I split this question into 2 different questions ? – Royi Namir Sep 27 '12 at 13:57
  • 2
    ideally you would not use javascript in the href attribute at all! – epascarello Sep 27 '12 at 13:57
  • @epascarello this is a MDN example ! – Royi Namir Sep 27 '12 at 13:58
  • @RobW sorry for this. ( language barrier )I tried to edit as much as I could in order to make it clearer. – Royi Namir Sep 27 '12 at 14:00
  • 2
    And MDN says you should not use it. :) "Note, however, that the javascript: pseudo protocol is discouraged over other alternatives, such as unobtrusive event handlers." [MDN Void](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2FSpecial%2Fvoid) – epascarello Sep 27 '12 at 14:01
  • @epascarello lol , yeah But isn't void(0) is === return undefined ? ( my second question) and please answer in a normal answer So i'll have something to check. – Royi Namir Sep 27 '12 at 14:03

2 Answers2

6

This will not work properly:

href="javascript:return false;"

because you are not in a function. You are thinking of this:

onclick="return false;"

which is correct since return false; is placed in a function. The false value tells the onclick to prevent the default behavior of the element.

For the return statement to work in an href attribute, you'd need a full function.

href="javascript:(function() { return false; })();"

but that's just long and ugly, and as the comments note, JavaScript in an href is generally discouraged.

EDIT: I just learned something. Having a non undefined expression as above seems to replace the elements with the return value (at least in Firefox). I'm not entirely familiar with the full ramifications of using JavaScript in an href, because I never do it.


Yes, this:

return undefined;

returns exactly the same thing this:

return void 0;

as long as the undefined variable has not been redefined or shadowed by some other value.

But while they may return the same thing, it's not entirely accurate to say they are the same thing, because:

  • undefined is a global variable whose default value is the undefined primitive

  • void is an unary operator that will replace the return value of its operand with the undefined primitive

So they both result in the undefined primitive, but they do so in a very different way.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
4

If you specify javascript: something as a value of href attribute, this something will be evaluated when someone activates that link.

The result of this evaluation will be checked: if its typeof evaluates to 'undefined' string, nothing will happen; if not, the page will be reloaded with the evaluation's result as its content:

<a href="javascript: void(0);">Nothing to see here, move along...</a>
<a href="javascript: undefined;">No, still nothing...</a>
<a href="javascript: prompt('Where would you like to go today?');">Check this out!</a>

The first two links here will do basically nothing. But the third one is quite more interesting: whatever you enter in prompt will be shown to you - even an empty string! But that's not all: if you click Cancel, you would still see a new page - with null printed (as cancelled prompt returns null, and, as you probably know, typeof null is in fact object; and null converted to String is, well 'null').

It could get more interesting:

<a href="javascript: window.undefined = 333; void(0);">What happens here?</a>
<a href="javascript: window.undefined = 333; undefined;">And here?</a>

Well, here we still get nothing at the first link. But second link will show you 333 in IE8. :)

And that's, I suppose, answers your second question as well: typeof void(0) will always be undefined. typeof undefined could give you dragons if someone decided it's a good idea to reassign them to window.undefined. )

... And yes, javascript: return false; is just wrong: you cannot return from non-function environment. You probably confused it with onclick: return false, but that's completely another story. )

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Well, I knew it'd take too much time to write this all. ) Just hope that all this stuff will happen to help someone. ) – raina77ow Sep 27 '12 at 14:29