Use switch statement with string value in JavaScript

Description

The following code shows how to use switch statement with string value.

Example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
<!--from  ww  w  .j a v  a2  s. co  m-->
var color = "green";
switch (color)
{
case "red":
document.write("The car is red.");
break;
case "blue":
document.write("The car is blue.");
break;
case "green":
document.write("The car is green.");
break;
default:
document.write("The car is purple.");
}
</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use switch statement with string value in JavaScript
Home »
  Javascript Tutorial »
    Statement »
      Switch
Javascript Tutorial Switch
Use Switch statement on day of week value r...
Use switch statement inside a for loop in J...
Use switch statement with string value in J...
Use switch statement with true false value ...