Welcome Guest! (App Version 3.1)

websitnero logo name

Conditional Statements

These are those tools which are used to conditionally direct the flow of program. For example, a person can either get the movie ticket or not. So, if he gets it he will watch movie, else he might go shopping or some other place to spend time. Similarly, in JavaScript we have if-else; moreover Switch-Case provides similar situation and is helpful in 3 or more possibilities, so that the program does not have to go through all the if-else Statements taking more time.

Following are the 2 kinds of Conditional Statements:

  • If-Else
  • Switch-Case

If-Else

This is used when there are 3 or more options or flow of program that are possible instead of just two. Whatever is written in the Switch condition is to be matched with all the cases mentioned. If none is matched, then default block is executed.

Example

<button onclick="alpha()"> Click </button>

<script>
var i = 10; 
function alpha(){
  if(i == 10){ 
    alert("The value is 10.");
  }
  else{
    alert("The value is NOT 10.");
  }
}
</script> 

Switch-Case

If provides a certain condition fulfilling which the program will execute whatever is written in its parenthesis, otherwise it will do the instructions written in the else block

Example

<button onclick="alpha()"> Click </button>

<script>
var i = 3; 
function alpha(){
  switch(i){
    case 1: {
      alert("The value is one.");
      break;
    }
    case 2: {
      alert("The value is two.");
      break;
    }
    case 3: {
      alert("The value is three.");
      break;
    }
    default:{
      alert("The value is undefined.");
    }
  }
}
</script> 
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.