Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and why to 'return false' in JavaScript?

When and why to return false in JavaScript?

like image 899
zsharp Avatar asked May 12 '09 23:05

zsharp


People also ask

Why do we use 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.

What is the reason for using a return false statement in the following code?

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.

What is the difference between return true and return false in JavaScript?

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.

Why we use return false in jQuery?

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.


1 Answers

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.

like image 106
Chris Jester-Young Avatar answered Oct 26 '22 15:10

Chris Jester-Young