that's a little bit over my head (beginner : /)
what's a switch-case statement?
Main Topics
Browse All Topicsis there a shortcut for writting an if else statement?
ex. I know this is ashortcut for the if statement w/ : as the else - i.e. (browserName == "Netscape" && browserVer == 5) ? document.write("This is Netscape 6") : document.write("This is NOT Netscape 6") )
but what would be a shortcut for this?
var browser;
if (browserName == "Netscape" && browserVer == 5) {
browser = "nn6"; }
else if (browserName == "Netscape" && browserVer == 4){
browser = "nn4";}
Thanks!!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can nest your ? : statements.
For example:
document.write(x == 6 ? "1" : x == 5 ? "2" : "3");
This is the same as:
if(x == 6) {
document.write("1");
}
else if (x == 5) {
document.write("2");
}
else {
document.write("3");
}
Only problem is that you always have to have a final ":", meaning this would give you an error:
document.write(x == 6 ? "1" : x == 5 ? "2");
Because (x == 5 ? "2") does not have an else (":") part.
-Xdoc
If by shortcut you mean a one line code, yes there is. The important thing with the (condition) ? : ; if else statement is that it needs to have something after the : to work. For example something like this works:
(2 == 3) ? alert('No') : ((2 == 2) ? alert('Yes'): 0);
This is similar to:
if (2 == 3) {
alert ('No')
} else if (2 ==2) {
alert ('Yes')
}
The '0' after the last ':' is just there to avoid errors. So your example could be rewritten like this.
var browser;
(browserName == "Netscape" && browserVer == 5) ? browser = "nn6" : ((browserName == "Netscape" && browserVer == 4) ? browser = "nn4" : 0);
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
In the absence of responses, I may recommend DELETE unless it is clear
to me that it has value as a PAQ. Silence = you don't care
</note>
Cd&
Business Accounts
Answer for Membership
by: weaponXPosted on 2002-12-27 at 12:33:30ID: 7637336
yes there is...how short I can't say for sure
set a new variable bType to take in the browserName and ver
then run a switch/case statement to handle the input.
like...
<script language="JavaScript1.2">
var browser
switch(browser)
{
case (browser="nn6"):
window.location="nn6.htm"
break
case (browser="nn4"):
window.location="nn4.htm"
break
</script>
I hope this helps you!