Joan
Joan

Reputation: 51

Is it correct to execute a function inside IF else statement?

The isCheck() function returns false if not radio buttons have been checked.

if (isCheck() === false) {
        i = 0;
        return i;
      }

Upvotes: 2

Views: 3841

Answers (4)

userfi
userfi

Reputation: 165

yes it works. alternatively you could use ternary operator using that syntax:

test ? expression1 : expression2

for example :

 //init var i with some value
 var i = 1;
 function isCheck(){ return false;}

 i = isCheck() === false ? 0 : i;
 return i;

or simple:

 //before you should initialize i
 return !isCheck()  ? 0 : i ;

Upvotes: 2

HudsonPH
HudsonPH

Reputation: 1868

Yes it is correct

enter image description here

You dont need to create a variable only to receive a bool and later do a validation you can do directly.

Upvotes: 0

brk
brk

Reputation: 50291

There is no wrong in executing a function inside if conditional statement.

For such case you can use ternary operator

var i=-1; // Note var key word & initialized with some value
  isCheck() === false ? (i=0):(i=someOtherVal)

Upvotes: 1

user6538437
user6538437

Reputation:

I don't see why not, some will say its not conventional but it should work just fine!

Upvotes: 0

Related Questions