2

Consider the following code:

if(boolean) return;

What is the purpose if this condition?

Is it logically the same as:

if(boolean) { return true; }

Thank you for answering!

2
  • 1
    Without more context it's impossible to say. Well, the direct answer is "no", because it's not the same at all, but you probably are asking a more involved question. Commented Apr 10, 2018 at 0:29
  • Basically if a certain condition is met, stop evaluating the function. Commented Apr 10, 2018 at 0:35

7 Answers 7

5

return; with no value is equivalent to return undefined;. But it's usually used in functions that aren't expected to return a value, so it just means "exit the function now".

So that code is equivalent to:

if (boolean) {
    return undefined;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Basically, in the first case you don't care about the return value: return by itself is often used to stop a function when a particular condition is met.

The second case if radically different: return true means you're interested in the return value of a function: for example, a function called isEmpty() that checks if a given list contains at least one element is expected to return a value that can be either true or false. In this case, the return statement alone would have no meaning.

Comments

0

if(boolean) { return true; } means if a condition is correct return true

if(boolean) return;

means if a condition is true stop Its used for different purpose, an example is validation, if a condition doesnt meet the criteria of a function just stop running.

Comments

0

The first one executes the if statement and returns back an "undefined" return call irrespective of the result from the if statement. The second one returns a true Boolean only if the if statement is true.

Comments

0

Let consider an example, you have a text box and a submit button. You want to validate the textbox for not being submitted with empty.

function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out"); return;  }}

Try submitting the form without a value in txt box.

Case 1 (return) : it will show the alert but won't restrict submission. Case 2(return true) : same as above. Case 3 (return false) : finally it will restrict the submission. First to behaves like similar because return; will return undefined which is anything but not false.

Comments

0

Return statement interrupts the execution of function and leave the current function with a value or undefined value.

In First case the return statement interrupts the execution of function and send the Boolean value.

function returnValue(){
         return true;
    }

returnWithValue();// this function hold the Boolean value
var value = returnWithValue();//true

In second case the return statement interrupts the execution of function and send the undefined value because we haven't provide value. The variable which hold the return value is created but not assigned any value.

function returnWithoutValue(){
      return;
}

returnWithoutValue();// this function hold the undefined value because we have not send any value var value = returnWithoutValue();//undefined

Comments

0

In the First Case you are returning undefined, the "if" condition is useless and when no return statement is specified, undefined is returned.

In the Second Case again "if" the condition is redundant when you return a boolean, you only need to return the condition (in this case is also boolean)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.