/    Sign up×
Community /Pin to ProfileBookmark

Ok, after a long time of just not really taking the initiative to truly read examples to understand, I have to ask…
What is all this [B]return[/B] business about?

Return false stops a form from carrying out, right? Something like that. Return true? What is this return somefunctionname() all about?

The whole situation feels the same as when I finally figured out how to use arguments in my web pages. Like I’m totally missing out on something great and grand in Javascript.

I’ll be the first to admit I don’t know all there is to know about Javascript ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@boojumMar 07.2003 — return is found in every programming language ive ever seen. it just returns that line. example:

function Somefunction(){

return blah();

}

X= Somefunction();

Somefunction is executed, all it does is execute blah() and return to what ever is calling it (in this case, the instance of Somefunction in the last line) whatever is returned to it from blah after blah executes. so if this is blah():

function blah(){

Z=1+1;

return 3;

}

first Z is created globably if it hasnt been globably declared already, then Z is given the value 2, then blah returns the value 3 to Somefunction which returns 3 to its calling instance, which assigns 3 to the global variable X. so the whole example is simplified to:

Z=2

X=3
Copy linkTweet thisAlerts:
@gil_davisMar 07.2003 — The return statement is a function terminator. No matter where in the function you place "return", the function stops, and execution resumes at the next instruction in the function that called it.

The return statement has an optional clause that allows a function to return a value to the calling function. Many built-in functions return a value (e.g.: Math.floor(1.25) returns 1, isNaN("y") returns true).

Your question refers more to event handling than to the return statement. When you define an event handler, that handler is inserted in front of the default handler for that particular object (e.g.: onclick in an anchor is inserted before the normal navigation event caused by clicking the anchor). The event handler gives you the option to control the next action by returning a boolean value to the event. If you return null or true to an event, you signify that you have handled your part and it is ok for the next handler to continue (the default action). If you return false to an event, you stop any further event processing for that object. This gets more complicated in newer browsers that bubble events to other objects, but there are techniques for handling that action as well.

You asked about forms submission. People that write form validation scripts use the onsubmit event for a form to tell when the user has decided he is finished filling out the form and has submitted it for processing. The function usually checks for required fields and formatting of special fields, and returns false if the form does not contain correct information or format or true if everything appears legitimate. Returning false will cancel the submit action, while returning true will allow the submit to continue.

<form onsubmit="return false"> simply disables submission of the form. <form onsubmit="return validate()"> would call the function validate() expecting a boolean value when the function is finished. If validate() returns true, the form will submit; if false, the submit will be canceled (some people don't use return in the form field, and probably wonder why their form gets submitted even with errors, or gets submitted more than once - some number of times with an error and then once correct, a common mistake).
×

Success!

Help @Nicodemas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...