Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript if statement in switch statement

Posted on 2013-12-19
6
Medium Priority
?
365 Views
Last Modified: 2013-12-19
Good morning expert,

Building app that should have an if statement in a switch statement.
Not having much luck so tried paring down to the bare bones.

This works

 var alpha = true;
    if (alpha === true)
    {document.write("Alpha is true")
    }
But when I put it in a switch statement, as copied below
it breaks.

Thanks

Allen in Dallas

<!DOCTYPE html>
<html>
<HEAD>
<title>JS Conditional w/ Switch Statement</title>

<style type="text/css">
 body {font-family: arial;}
 </style>
</HEAD>

<body>

This JavaScript uses an If statement in a switch statement<br>
<br><br>
<script>

 var alpha = true;

switch (beta)
  {
  case 0:
    if (alpha == true)
    {document.write("Alpha is true")}
    break;
  case 1:
    if (alpha != true)
    {document.write("Alpha is not true")}
    break;
}
</script>

</body>
</html>

Open in new window

0
Comment
Question by:Allen Pitts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Accepted Solution

by:
ITTallahassee earned 2000 total points
ID: 39729216
It is valid to put an if statement inside of switch, but to start you haven't declared beta or what beta is (switch variable).  And since you don't have a "Default" case and "Beta" isn't declared nothing is happening. Did you mean to leave your script outside of the <head>?
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39729230
I'm not sure what you mean by "it breaks".
If you mean it is not running, that is because you are switching based on the value of beta, but never setting beta.
If you set beta to 0 or 1 all works well.
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39729235
Sorry, ITTallahassee, I did not refresh my screen before submitting my comment.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Iphonehelper
ID: 39729278
hello,
i have no idea about this but i am learning java and i hope i can help
try this,

maybe it is not working because you did not declare beta.

<!DOCTYPE html>
<html>
<HEAD>
<title>JS Conditional w/ Switch Statement</title>

<style type="text/css">
 body {font-family: arial;}
 </style>
</HEAD>

<body>

This JavaScript uses an If statement in a switch statement<br>
<br><br>
<script>

 var alpha = true;

switch (alpha)
  {
  case true:
    if (alpha == true)
    {document.write("Alpha is true")}
    break;
  case false:
    if (alpha != true)
    {document.write("Alpha is not true")}
    break;
}
</script>

</body>
</html>
0
 
LVL 5

Expert Comment

by:Iphonehelper
ID: 39729284
oh sorry guys i also didnt refresh my page
0
 

Author Closing Comment

by:Allen Pitts
ID: 39729307
Excellent explanation
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question