Solved

Javascript if statement in switch statement

Posted on 2013-12-19
6
361 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:9apit
[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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:9apit
ID: 39729307
Excellent explanation
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

617 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