HTML 5 TutorialsImplementing Conditional Statements in JavaScript

Implementing Conditional Statements in JavaScript

In this session today we will study the conditional statements of java script which includes
if ,if. .else ,if…..else if..else and switch statement.

  • To implement the examples of conditional statements in JavaScript follow the steps given below :
    1. To implement the above mentioned conditional statements write the following code in Notepad++ or any other text editor.

      <!Doctype html>
      <html>
      	<head>
      		<title>Javascript Conditionals</title>
      	</head>
      	<body>
      
      	
      	
      	<h1>This is an if statement</h1>
      	<script>
      	x = 5;
      	if(x > 2){
      		document.write(+x+'  is greater');
      	}
      	</script>
        <br />
        <br />
       
      
      
      
      <h1>This is an if else statement</h1>
      
      <script>
      	x = 1;
      	if(x > 2){
      		document.write(+x+ ' is greater');
      	} else {
      		document.write(+x+ ' is less');
      	}
      </script>
      
      <br />
      
      <br />
      
      <h1>This is an if else statement with relationaly operator</h1>
      <script>
      	x = 1;
      	if(x > 2 && x < 10){
      		document.write('True');
      	} else {
      		document.write('False');
      	}
      </script>
      <br />
      <br />
      
      
      <h1>Thi is an if statement with or operator   </h1>
      <script>
      	x = 20;
      	if(x < 10 || x == 20){
      		document.write('True');
      	} else {
      		document.write('False');
      	}
      </script>
      
      <br />
      <br />
      
      
      <h1> This is an if...else if...else statement</h1>
      <script>
      	x = 5;
      	if(x == 2){
      		document.write('x is 2');
      	} else if(x == 5) {
      		document.write('x is 5');
      	} else {
      		document.write('x is not 2 or 5');
      	}
      </script>
      
      <br />
      <br />
      
      
      <h1> This is an switch statement</h1>
      <script>
      	x = 4;
      	switch(x){
      		case 2:
      			document.write('x is 2');
      			break;
      		case 5:
      			document.write('x is 5');
      			break;
      		default:
      			document.write('x is not 2 or 5');
      			break;
      	}
      </script>
      
      <br />
      
      </script>
      
      </body>
      </html>
      
    2. After writing the code you will have the following view of your Notepad++ window :
    3. v1
      v2
      v3
      v4

    4. When you open your file in the browser you will have the following output :
    5. v5

    6. Thus we have understood the use and implementation of conditional statements in JavaScript.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Exclusive content

    - Advertisement -

    Latest article

    21,501FansLike
    4,210FollowersFollow
    104,000SubscribersSubscribe

    More article

    - Advertisement -