2

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.

7
  • Your writing is all over the place, and your "please 2 questions" don't help to clarify things either... Commented Sep 27, 2012 at 13:56
  • @BoltClock Sorry, should I split this question into 2 different questions ? Commented Sep 27, 2012 at 13:57
  • 2
    ideally you would not use javascript in the href attribute at all! Commented Sep 27, 2012 at 13:57
  • @epascarello this is a MDN example ! Commented Sep 27, 2012 at 13:58
  • 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 Commented Sep 27, 2012 at 14:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

17 Comments

@RoyiNamir No it doesn't, look at the error console. Just because the javascript fails, doesn't mean it works.
yeah I looked at it and there were errors - Thanks for a great answer.
by the way I think onclick="return false;" not only prevents the default behavior, but also stops propagation (bubbling) of the event. href="#" is just a link to an empty hash, usually the default behavior is to jump to the top of the page; without reloading
@Chad: that's just in jQuery (the stopPropagation). jQuery's behavior is non-standard.
@RoyiNamir: It's just a way to make the <a> element display as a link (usually blue and underlined) without having the href lead to anywhere. Without the href, the <a> text will look like any other text, but with the href, when you click it, the browser wants to take you somewhere. This is just a hack so you get the appearance of a link, but not the behavior.
|
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. )

1 Comment

Well, I knew it'd take too much time to write this all. ) Just hope that all this stuff will happen to help someone. )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.