When and why to 'return false' in JavaScript?
Often, in event handlers, such as onsubmit
, returning false is a way to tell the event to not actually fire. So, say, in the onsubmit
case, this would mean that the form is not submitted.
I'm guessing that you're referring to the fact that you often have to put a 'return false;' statement in your event handlers, i.e.
<a href="#" onclick="doSomeFunction(); return false;">...
The 'return false;' in this case stops the browser from jumping to the current location, as indicated by the href="#" - instead, only doSomeFunction() is executed. It's useful for when you want to add events to anchor tags, but don't want the browser jumping up and down to each anchor on each click