How to use Conditional Statements in JavaScript? ,Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher
How to use Conditional Statements in JavaScript?

 
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:
<script type="text/javascript">
//Write a "Good Afternoon" welcome if
//the time is greater than 12
var d=new Date();
var time=d.getHours();
if (time>12) 
{
document.write("<b>Good Afternoon</b>");
}
</script>
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:
<script type="text/javascript">
//If the time is greater than 12,
//you will get a "Good Afternoon" welcome.
//Otherwise you will get a "Good day" welcome.
var d = new Date();
var time = d.getHours();
if (time > 12) 
{
document.write("Good Afternoon");
}
else
{
document.write("Good day");
}
</script>
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:
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good Afternoon</b>");
}
else
{
document.write("<b>R4R Welcomes You!</b>");
}
</script>
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:
<script type="text/javascript">
//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("It is Sunday");
  break;
case 1:
  document.write("It is Monday");
  break;
case 2:
  document.write("It is Tuesday");
  break;
case 3:
  document.write("It is Wednesday");
  break;
case 4:
  document.write("It is Thursday");
  break;
case 5:
  document.write("It is Friday");
  break;
case 6:
  document.write("It is Saturday");
  break;
default:
  document.write("Press only numbers from 0 to 6 ");
}
</script>
 

More interview questions and answers

What is the difference between the Font and FontMetrics classes?

What is the relationship between an event-listener interface and an event-adapter class?

How can a GUI component handle its own events?

What is the difference between the paint() and repaint() methods?

What interface is extended by AWT event listeners?

What is Canvas ?

What is default Look-and-Feel of a Swing Component?

What are the features of JFC?

What does x mean in javax.swing?

What are invisible components?

What is the default layout for a ContentPane in JFC?

What does Realized mean?

Is Java and JavaScript are same or not?

When we use JavaScript?

What are you understand about ECMAScript?

How to use JavaScript on HTML pages?

How to use comments in JavaScript?

In HTML where JavaScript will executed?

How to use an External JavaScript?

Is JavaScript Case-Sensitive or not?

How you define JavaScript Statement?

How you define JavaScript Code?

What is difference between Swing and JSF?

Why does JComponent class have add() and remove() methods but Component class does not?

What method is used to specify a container\'s layout?

What is the difference between AWT and SWT?

What is the difference between JFC & WFC?

What is a convertor?

What is the difference between a Canvas and a Scroll Pane?

What is the purpose of the enableEvents() method?

What is the difference between a MenuItem and a CheckboxMenuItem?

Which is the super class of all event classes?

How the Canvas class and the Graphics class are related?

What is the difference between a Window and a Frame?

What is the relationship between clipping and repainting?

What advantage do Java\'s layout managers provide over traditional windowing systems?

When should the method invokeLater() be used?

How you define JavaScript Blocks?

How we use JavaScript Comments?

How you define Javascript Variable?

How to create variable and assign value to those variables in JavaScript?

What is the diff b/w choice and the List ?

How you define JavaScript Operator?

Which component subclass is used for the drawing and painting ?

Which containers may have a menu bar?

Which class is the immediate supar class of the menuComponent class?

How you define Comparison and Logical Operators?

What is the relationship b/w the Canvas class and the Graphics Class?

How are the elements of a borderLayout organized?

How are the elements of a GridBagLayout organized?

How to use Conditional Statements in JavaScript?

Difference b/w paint() and paintComponent()?

Give us the Name of the List LayoutManagers in Java?

How you define Popup Boxes in JavaScript?

How we use functions in JavaScript?

How to define function in JavaScript?

What is the Collections API?

What is the List interface?

What is the Vector class?

What is an Iterator interface?

Which java.util classes and interfaces support event handling?

What is the GregorianCalendar class?

What is the Local class?

What is the SimpleTimeZone class?

What is the Map interface?

What is the highest-level event class of the event-delegation model?

What is the Collection interface?

What is the Set interface?

What is the typical use of Hashtable?

We trying to store an object using a key in a Hashtable. And some other object already exists in that location, then what will happen? The existing object will be overwritten? Or the new object will be stored elsewhere?

What is the difference between the size and capacity of a Vector?

Can a vector contain heterogenous objects?

What is the Lifetime of JavaScript variables?

Can a ArrayList contain heterogenous objects?

What is an enumeration?

Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?

How we use Loop in JavaScript?

How we use Break and Continue Statement in JavaScript?

How you define for..in statement in JavaScript?

How you define Events in JavaScript?

How you use try..catch statement in JavaScrpt?

How to use throw statement in JavaScript?

What is HashMap and Map?

What is the significance of ListIterator?

What is the difference b/w Iterator and ListIterator?

Difference between HashMap and HashTable?

What is the difference between set and list?

Difference between Vector and ArrayList? What is the Vector class?

What is an Iterator interface? Is Iterator a Class or Interface? What is its use?

How can we access elements of a collection?

What is the Set interface?

What is the difference between a queue and a stack?

What is the Map interface ?

What is the Properties class?

Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?

How can we use hashset in collection interface?

What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection?

What is difference between array & arraylist?

Can you limit the initial capacity of vector in java?

What method should the key class of Hashmap override?

R4R provides free online tutolrails ,examples for Freshers and experts .We cover core java, tutorials, spring, Hibernate, python, struts, struts 2,c language, spring boot,PHP,MYSQL,C#,ASP.NET,WPF,WCF,Photoshop , JSP, programming, example, Interview questions and answer