How to use Conditional Statements in JavaScript?

How to use Conditional Statements in JavaScript?

<pre>&nbsp;</pre>
<pre>Using Conditional statements we can apply different actions on different conditions.
JavaScript provide has four different conditional statements.
1.if statement: This statement is used to execute some specific code when specific condition is true.
Syntax:
if (condition)
{
this code will executed if condition is true
}
Example:
&lt;script type=&quot;text/javascript&quot;&gt;
//Write a &quot;Good Afternoon&quot; welcome if
//the time is greater than 12
var d=new Date();
var time=d.getHours();
if (time&gt;12) 
{
document.write(&quot;&lt;b&gt;Good Afternoon&lt;/b&gt;&quot;);
}
&lt;/script&gt;
2.if...else statement: If condition is true than execute some code else execute different code.   
Syntax:
if (condition)
{
This code will executed if condition is true
}
else
{
This code will executed if condition is not true
}
Example:
&lt;script type=&quot;text/javascript&quot;&gt;
//If the time is greater than 12,
//you will get a &quot;Good Afternoon&quot; welcome.
//Otherwise you will get a &quot;Good day&quot; welcome.
var d = new Date();
var time = d.getHours();
if (time &gt; 12) 
{
document.write(&quot;Good Afternoon&quot;);
}
else
{
document.write(&quot;Good day&quot;);
}
&lt;/script&gt;
3.if...else if....else statement: Using this statement we can select one of many blocks of code to be executed. 
Syntax:
if (condition1)
{
This code will executed if condition1 is true
}
else if (condition2)
{
This code will executed if condition2 is true
}
else
{
This code will executed if condition1 and
condition2 are not true
}
Example:
&lt;script type=&quot;text/javascript&quot;&gt;
var d = new Date()
var time = d.getHours()
if (time&lt;10)
{
document.write(&quot;&lt;b&gt;Good morning&lt;/b&gt;&quot;);
}
else if (time&gt;10 &amp;&amp; time&lt;16)
{
document.write(&quot;&lt;b&gt;Good Afternoon&lt;/b&gt;&quot;);
}
else
{
document.write(&quot;&lt;b&gt;R4R Welcomes You!&lt;/b&gt;&quot;);
}
&lt;/script&gt;
4.switch statement: Using this statement we can select one of many blocks of code to be executed. 
Syntax;
switch(i)
{
case 1:
  execute code block 1
  break;    
case 2:
  execute code block 2
  break;
default:
  code to be executed if n is
  different from case 1 and 2
}
Example:
&lt;script type=&quot;text/javascript&quot;&gt;
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6.

var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 0:
  document.write(&quot;It is Sunday&quot;);
  break;
case 1:
  document.write(&quot;It is Monday&quot;);
  break;
case 2:
  document.write(&quot;It is Tuesday&quot;);
  break;
case 3:
  document.write(&quot;It is Wednesday&quot;);
  break;
case 4:
  document.write(&quot;It is Thursday&quot;);
  break;
case 5:
  document.write(&quot;It is Friday&quot;);
  break;
case 6:
  document.write(&quot;It is Saturday&quot;);
  break;
default:
  document.write(&quot;Press only numbers from 0 to 6 &quot;);
}
&lt;/script&gt;</pre>
<pre>&nbsp;</pre>

By:Vivek
Date:2019-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related JAVASCRIPT Subjective Links

JAVASCRIPT Subjective interview questions and answers for experienced and fresher