15

I have written a form validation using JS which ends with return(true);

function check() {
  ....validation code
  return(true);
}

All I want is, need to check if check() function returns true, I want to execute another function.

Code I have tried is as follows:

if(check() === true) {
  function() {
    //Another function code
  }
}
4
  • 1
    check can return just true also you don't need to use === if(check()) is fine Commented Jan 21, 2015 at 14:30
  • 1
    Adding to what Alex said, Just do if (check()) { ... } Commented Jan 21, 2015 at 14:30
  • 1
    I think this question may be off-topic because the answer seems to be in the question itself. Commented Jan 21, 2015 at 14:34
  • With respect to that function you want to execute if check() evaluates as true - is it actually defined somewhere else outside that conditional? Commented Jan 21, 2015 at 14:36

5 Answers 5

35

You should use return true; and your if statement doesn't need the === true comparison.

function check() {
  //validation code
  return true;
}

if(check()) {
  //Another function code
 }

JSFIDDLE

Sign up to request clarification or add additional context in comments.

2 Comments

Was just trying to maintain consistency with the direction the OPs code was heading... its really probably not necessary.
"All I want is, to check if check() returns true, to execute another function." The OP's code was wrong if that's what he wants to do. Fix the code to match the requirement, instead of changing the results to match the OP's code.
12

First of all, return is not a function, you can just do this:

return true;

Now, to only execute myFunction if check returns true, you can do this:

check() && myFunction()

This is shorthand for:

if(check()){
    myFunction();
}

You don't need to compare the return value of check with true. It's already an boolean.

Now, instead of myFunction(), you can have any JavaScript code in that if statement. If you actually want to use, for example, myFunction, you have to make sure you've defined it somewhere, first:

function myFunction() {
    // Do stuff...
}

Comments

0

You just need to modify your first code snippet. return is a keyword, what you are trying to do is to execute it as a function.

function check() {
....validation code
return true;
}

You'll need to change your 2nd snippet slightly, to execute the function too however... The simplest way is to wrap it as an anonymous function using curly braces:

if(check()) {
       (function() {
                    //Another function code
       })();
 }

5 Comments

That if is completely useless... function statements like that get hoisted, there is no block scope in JS, just function scope. Try this: if(false){function test(){console.log(1)}} test();
@Cerbrus: You sample isn't the same as what I included. The function declaration may get hoisted up, but the execution doesn't - see jsfiddle.net/IPWright83/3zn8m6vc
My code sample matches what was in your answer before your edit. Now you have an utterly useless IIFE in an if statement. Take a look at what the OP requested: "All I want is, to check if check() returns true, to execute another function."
@Cerbrus - true, the function doesn't need to be declared there.
My point is that this answer is completely missing the point of the question. The OP just wants to run a function if a statement is true. IIFE's have no business here. Not to mention that return(something) doesn't try to execute anything as a function. That's just some brackets wrapped around a return statement's parameter.
-2

You're not calling the function in your affirmative clause, only declaring it. To call an anonymous function do this:

(function (){...})()

Comments

-2

You could type

$this.myFunction=function(){
//code here
}

and to execute some code if a the myFunction function is true, you could use booleans such as e.g.

if(//your function is true){

and so on

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.