I am really new to javascript, and stumbled upon the return keyword. Basically, what is the difference in terms of these 2 statements?
<input type="checkbox" onclick="doAlert()" />
vs
<input type="checkbox" onclick="return doAlert();" />
Essentially, both returned the same results and called the function, but is there more to it? Any help greatly appreciated :). Thanks!
Returning false from the function, will abort the effect of the checking. Because the native of functions that written hardcoded into html properties (it became some new local function), writing the html without the word “return” will just run the function, and lose its returning value, as if you’ve wrote:
Function
some_local_functionwon’t return any value, althoughdoAlertreturns.When you write “return”, it’s like you wrote the second function like this:
which preserves the returning value of doAlert, whatever it will be. If it’s true – the action will perform (the checkbox will be checked) – otherwise – it will cancel.
You can see live expamle here: http://jsfiddle.net/RaBfM/1/