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
  }
}
JRulle
  • 7,448
  • 6
  • 39
  • 61
Anand S
  • 153
  • 1
  • 1
  • 6

5 Answers5

33

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

JRulle
  • 7,448
  • 6
  • 39
  • 61
  • 1
    Was just trying to maintain consistency with the direction the OPs code was heading... its really probably not necessary. – JRulle Jan 21 '15 at 14:39
  • _"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. – Cerbrus Jan 21 '15 at 14:40
10

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...
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
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
       })();
 }
Ian
  • 33,605
  • 26
  • 118
  • 198
  • 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 Jan 21 '15 at 14:38
  • @Cerbrus: You sample isn't the same as what I included. The function declaration may get hoisted up, but the execution doesn't - see http://jsfiddle.net/IPWright83/3zn8m6vc/ – Ian Jan 21 '15 at 14:42
  • 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 Jan 21 '15 at 14:42
  • @Cerbrus - true, the function doesn't *need* to be declared there. – Ian Jan 21 '15 at 14:44
  • 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. – Cerbrus Jan 21 '15 at 14:46
-2

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

(function (){...})()
Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
-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