Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
JavaScript
Introduction
Variables & Arrays
Functions
If Statements
Case Statements
Loop Statements
Operators
Form Validation
Alert
Date Objects
Window Objects
Javascript Books
Books @ CA Amz
Books @ UK Amz
Books @ US Amz

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing

Javascript case statement

Case statement can be used to control different possibilities of a condition. Use of case statement is recommended when single condition could have range of possibilities. For example, getDate is a method that could return any day of the week. Case statement can be used to determine and display a message related to current day.

The syntax for case statement is as follows:
switch(condition)
{
 case value:
  Action statement
}
The case value is executed when the condition is true. If no match is found, then default condition is executed if specified.

The following example explains use of case statement to get current day:
<html>
<body>
<script type="text/javascript">
<!--
var dat = new Date()
today=dat.getDay()
switch (today)
{
case 2:
  alert("It's Monday")
  break
case 3:
  alert("It's Tuesday")
  break
case 4:
  alert("It's Wednesday")
  break
case 5:
  alert("It's Thursday")
  break
case 6:
  alert("It's Friday")
  break
case 7:
  alert("It's Saturday")
  break
case 1:
  alert("It's Sunday")
  break
}
//-->
</script>
</body>
</html>
In this example, we declared a variable dat and set it to date object. getDay is a method that has access to the days of the week in numbers 1-7. switch sets the condition to test and case tests the value and executes when it's true. break breaks the condition.
Since the condition is always true, hence everyday is a day; there is no need of specifying default statement to execute when there is no match. This example executes when the page is loaded since it's in the body section of the document.

The following example asks the user to enter a number between 1 and 3. If wrong number entered, default value is executed.

<body>
<script type="text/javascript">
var n=prompt("Enter a number between 1 to 3:",0)
switch(n)
{
case(n="1"):
  alert("You typed "+n);
break
case(n="2"):
  alert("You typed "+n);
break
case(n="3"):
  alert("You typed "+n);
break
default:
  alert("Wrong number");
break
}
</script>
</body>

If statement Loops
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware