If Else statements allow you to check if a certain condition is true, and do something, and if it's not true, do something else instead.
You can use the if statement on it's own:
<script>
condition=true;
if(condition){
alert('Condition is True');
}
</script>
Or you can use the if statement with an else statement:
<script>
condition=true;
if(condition){
alert('Condition is True');
}
else{
alert ('Condition is False');
}
</script>
You can even combine if & else statements together:
<script>
condition='c';
if(condition=='a'){
alert('Condition is A');
}
else if(condition=='b'){
alert('Condition is B');
}
else{
alert('Condition is C');
}
</script>
There's not a lot else to discuss regarding if else... it's that simple, and you'll use them alot!
But wait... there is a shorthand version of if else which is great for making quick yes/no decisions inline:
<script>
You simply place the thing you want to check in brackets (condition==true), followed by a question mark (?). This basically asks the question 'is the condition in the brakets true?', and if so executes the code in front of the colon (:), and if not it executes the code after the colon (:). I tend to wrap the whole question in another set of brackets just to make sure it's separated from any other code, but you don't have to!
condition=true;
alert('Condition is '+((condition)?'True':'False'));
</script>