When and why to return false
in JavaScript?
Web Developers use 'return false' in different ways. During form submission, if a particular entry is unfilled, return false is used to prevent the submission of the form.
Any code after return statement in a function will never be executed. It stops executing of function and make this function return value passed ( false in this case). Your function is "submit" event callback. If this callback returns false , form will not be submitted actually.
Using return causes your code to short-circuit and stop executing immediately. The first return statement immediately stops execution of our function and causes our function to return true . The code on line three: return false; is never executed.
When you return false; from an event handler it prevents the default action for that event and stops the event bubbling up through the DOM. It is effectively the same as calling both e. preventDefault and e. stopPropagation on the passed jQuery.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With