Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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

<script> function CallSomeFunction() {    //function body }  $(document).ready(function () {  callSomeFunction();  return false; }); 

and at other times:

callSomeFunction(); 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?

like image 301
oliverdejohnson Avatar asked Oct 03 '13 18:10

oliverdejohnson


People also ask

What is return true and return false?

returning true or false indicates that whether execution should continue or stop right there. So just an example <input type="button" onclick="return func();" /> Now if func() is defined like this function func() { // do something return false; }

Is return the same as return false?

No, return; is the same as return undefined; , which is the same as having a function with no return statement at all.

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

Return statements, in programming languages, are used to skip the currently executing function and return to the caller function. Return statements may/may not return any value. Below is the example of a return statement in JavaScript.

How is return used in JavaScript?

The return statement ends function execution and specifies a value to be returned to the function caller.

What is a return statement in JavaScript?

Return statements, in programming languages, are used to skip the currently executing function and return to the caller function. Return statements may/may not return any value. Below is the example of a return statement in JavaScript. Example 2: Similarly, we can return true or false in a simple JavaScript function.

Why does my JavaScript function return false when I click?

Your code makes no sense, maybe because it's out of context. The return false will return false to the click-event. That tells the browser to stop following events, like follow a link. It has nothing to do with the previous function call. Javascript runs from top to bottom more or less, so a line cannot affect a previous line.

What does it mean when a function returns false?

Developers use the return false in many different cases.For example, Depending upon boolean (true or false) value If a form field (fname) is empty, then function alerts a message, and returns false, to prevent the form from being submitted.

What is the difference between return true and return false?

Javascript runs from top to bottom more or less, so a line cannot affect a previous line. returning true or false indicates that whether execution should continue or stop right there. the click event will never get executed. On the contrary if return true is written then the click event will always be executed.


1 Answers

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> 

or

<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.

like image 98
Eric Hotinger Avatar answered Sep 20 '22 10:09

Eric Hotinger