If statement inside of an if statement In JavaScript

  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

Is it possible to run an "if/else" statement inside of an "if" parameter? If it is, I couldn't figure out the way. If that is impossible, is there a way to run a function() inside of another function or "if/else" statement?

(random) example:

JAVASCRIPT Code: [ Select ]
function myFunction()
{
if(x=true)
   {
   if(y<2){document.write("whatever");}
   else {alert("Text here");}
   }
else
   {
   alert("whatever");
   }
}
 
 
or...
 
function myFunction()
{
if(x<3)
   {
   aFunction()
   }
else
   {
   document.write("Anything");
   }
}
 
  1. function myFunction()
  2. {
  3. if(x=true)
  4.    {
  5.    if(y<2){document.write("whatever");}
  6.    else {alert("Text here");}
  7.    }
  8. else
  9.    {
  10.    alert("whatever");
  11.    }
  12. }
  13.  
  14.  
  15. or...
  16.  
  17. function myFunction()
  18. {
  19. if(x<3)
  20.    {
  21.    aFunction()
  22.    }
  23. else
  24.    {
  25.    document.write("Anything");
  26.    }
  27. }
  28.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

not quite sure what you mean but i think this might answer your question http://jsfiddle.net/Tueh8/1/
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

I think that link answers my question, but I don't fully understand ALL of the syntax in your example yet, but I will. So, thanks for posting it. =)

When I tried to do it the way I posted it, it wouldn't work, but it's also possible that I had some other syntax error in my site.

If there IS some specific reason that the syntax I posted up top wouldn't work, please point it out if you can.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

in your example i don't know what x or y are. are they variables set outside of the function or variables ment for the functions use only?
JAVASCRIPT Code: [ Select ]
// if the variables are ment to be used in the function only  in a case like
 
var x_var = false;
var y_var = 5;
myFunction(x_var, y_var);
 
// then the below line should look like
// function myFunction() {
function myFunction(x, y) {
 
    //if(x=true) { // This line is setting x to be true so it will always be true.
      if(x==true) { // This would be the correct syntax
       
        if(y<2) {
            document.write("whatever");
        } else {
            alert("Text here");
        }
    } else {
        alert("whatever");
    }
}
 
  1. // if the variables are ment to be used in the function only  in a case like
  2.  
  3. var x_var = false;
  4. var y_var = 5;
  5. myFunction(x_var, y_var);
  6.  
  7. // then the below line should look like
  8. // function myFunction() {
  9. function myFunction(x, y) {
  10.  
  11.     //if(x=true) { // This line is setting x to be true so it will always be true.
  12.       if(x==true) { // This would be the correct syntax
  13.        
  14.         if(y<2) {
  15.             document.write("whatever");
  16.         } else {
  17.             alert("Text here");
  18.         }
  19.     } else {
  20.         alert("whatever");
  21.     }
  22. }
  23.  


so assuming my assumptions are correct a correct working function would be

JAVASCRIPT Code: [ Select ]
 
function myFunction(x, y) {
      if(x==true) {
        if(y<2) {
            document.write("whatever");
        } else {
            alert("Text here");
        }
    } else {
        alert("whatever");
    }
}
 
var x_var = false;
var y_var = 5;
myFunction(x_var, y_var);
 
// OR if x and y are set outside would be  
 
function myFunction2() {
      if(x_var==true) {
        if(y_var<2) {
            document.write("whatever");
        } else {
            alert("Text here");
        }
    } else {
        alert("whatever");
    }
}
 
myFunction2();
 
  1.  
  2. function myFunction(x, y) {
  3.       if(x==true) {
  4.         if(y<2) {
  5.             document.write("whatever");
  6.         } else {
  7.             alert("Text here");
  8.         }
  9.     } else {
  10.         alert("whatever");
  11.     }
  12. }
  13.  
  14. var x_var = false;
  15. var y_var = 5;
  16. myFunction(x_var, y_var);
  17.  
  18. // OR if x and y are set outside would be  
  19.  
  20. function myFunction2() {
  21.       if(x_var==true) {
  22.         if(y_var<2) {
  23.             document.write("whatever");
  24.         } else {
  25.             alert("Text here");
  26.         }
  27.     } else {
  28.         alert("whatever");
  29.     }
  30. }
  31.  
  32. myFunction2();
  33.  


As seen here http://jsfiddle.net/mMSj2/
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

I think in the page I was doing, they were global variables. Oh yeah, and I did realize that my comparison operator was written incorrectly, sorry about that. That was irrelevant to my actual page. I didn't make that same syntax error there, when I was having the trouble.
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

I'm going to study the examples you posted, and do a little bit of research on how you were putting the variables in the function() parenthesis, because I haven't been doing that yet.
like function(a,b).

So, before I trouble you further, I'm going to look into this stuff.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 85 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.