oliverdejohnson oliverdejohnson - 1 year ago 80
Javascript Question

javascript return true or return false when and how to use it?

So I see a lot of JavaScript code (have written some myself) that does something like

function CallSomeFunction()
//function body

$(document).ready(function () {
return false;

and at other times:

return true;

Basically I've never understood the true use of the return true/false after function calls in JavaScript. I've just assumed its some kind of magic I need my functions to run well.

So please I'd like to know why? why do we use return true or return false after calling a JavaScript function?

Answer Source

I think a lot of times when you see this code, it's from people who are in the habit of event handlers for forms, buttons, inputs, and things of that sort.

Basically, when you have something like:

<form onsubmit="return callSomeFunction();"></form>


<a href="#" onclick="return callSomeFunction();"></a>`

and callSomeFunction() returns true, then the form or a will submit, otherwise it won't.

Other more obvious general purposes for returning true or false as a result of a function are because they are expected to return a boolean.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download