Basically, you have a value and a test.
The value could be a number (999) or a string ("female") or boolean (true).
The test is always going to be, "Is it true that the value is this condition?"
- Is the number 999?
- Is the string "female"?
- Is the boolean true?
Some of the statements you have available are:
- if (value == test)
- if (value == test) ... else
- if (value == test) ... else if (value == test)
- switch (value) ... case (test) ... default
The switch condition statement in Javascript lets you perform an action based on the value of a particular thing. Depending on your preferences, the switch statement may provide better understanding of your code than a series of if () ... else if () ... else if () ... else statements. For example, we'll sort some "food" into a few different types -- "fruit" or "vegetable" or "unknown":
var food = "cucumber";
switch (food) {
case "banana":
myType = "fruit";
break;
case "cucumber":
myType = "vegetable";
break;
case "orange":
myType = "fruit";
break;
case "carrot":
myType = "vegetable";
break;
default:
myType = "unknown";
}
alert (myType);
So, this should do a pop-up alert saying "vegetable";
Well, what if you are using numbers?
And the number you are using is not an exact value?
The number could be greater than, equal to or lower than a particular test value, right?
For example, we want to alert someone based on how much fuel is in their tank -- and the value we want to test is a percentage of the tank's fullness. Using switch on the tankFullnessPercentage won't work because we are only testing to see if a value is equal to the test value:
var tankFullnessPercentage = 8;
switch (tankFullnessPercentage) {
case 0:
output = "Your tank is empty";
break;
case 25:
output = "Your tank is nearly empty";
break;
case 50:
output = "Your tank is half empty";
break;
default:
output = "You have plenty of gas."
}
window.alert (output + " at " + tankFullnessPercentage + "%.");
This is going to alert the wrong message, because it will say "You have plenty of gas at 8%," when in reality, your tank is nearly empty.
Solution: How to Test for Greater Than or Less Than in a Switch Statement
What you want to test for is whether a condition is "true."
So, using the same example as above, we just need to change a couple of things around:
var tankFullnessPercentage = 8;
switch (true) {
case (tankFullnessPercentage <= 0):
output = "Your tank is empty";
break;
case (tankFullnessPercentage <= 25):
output = "Your tank is nearly empty";
break;
case (tankFullnessPercentage <= 50):
output = "Your tank is half empty";
break;
default:
output = "You have plenty of gas."
}
window.alert (output + " at " + tankFullnessPercentage + "%.");
So, this is the correct way to test for a range of values in a switch statement. At 8%, the alert will now say "Your tank is nearly empty at 8%."
This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got a good knowledge.
ReplyDeletePhp course in chennai
Great Article
DeleteAngular Final Year Project Ideas for Computer Science
Project Centers in Chennai
Thanks for sharing the details! thanks for sharing information,nice article.
ReplyDeletei would like to more information from your side!
please added more then tips!Am working in
websphere training in hyderabad
Starting impressions are significant yet similarly so is the limit of your logo to talk with clients of your business and to extend to them the nature of what you bring to the table. So once it comes to choosing your ideal logo configuration there is a great deal to consider! logo design service
ReplyDelete
ReplyDeleteGreat Article
B.Tech Final Year Projects for CSE in JavaScript
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Nice blog Thank you very much for the information you shared.
ReplyDeleteweb analytics service providers
analytics companies in bangalore
top analytics companies in india
The promotion around Big Data is only that, BIG. Despite the fact that Big Data is anticipated to be the future grain for all investigation. Data Analytics Course in Bangalore
ReplyDeleteVery Informative Article
ReplyDeleteData Science Interview Questions
Woh Everyone loves you , bookmarked ! My partner and i take issue in your last point. https://royalcbd.com/product/cbd-roll-on-gel/
ReplyDeleteNice post and I want more different ideas from your excellent blog...!
ReplyDeleteLinux Training in Chennai
Graphic Design Courses in Chennai
Social Media Marketing Courses in Chennai
Placement Training in Chennai
Soft Skills Training in Chennai
Oracle Training in Chennai
Appium Training in Chennai
Spark Training in Chennai
Pega Training in Chennai
Tableau Training in Chennai
Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeletedata science course
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
ReplyDeleteSimple Linear Regression
Correlation vs Covariance
Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteCorrelation vs Covariance
Simple linear regression
data science interview questions
Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
ReplyDeleteSimple Linear Regression
Correlation vs covariance
KNN Algorithm
Logistic Regression explained
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
ReplyDeletedata science interview questions
very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteCorrelation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained
I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
ReplyDeleteSimple Linear Regression
Correlation vs Covariance
very well explained .I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteSimple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained
Wonderful article for the people who need useful information about this course.
ReplyDeleteseo mistakes
future scope of machine learning
angularjs
automation advantages
angularjs interview questions for experienced
angularjs interview questions and answers for freshers
software testing interview questions and answers pdf
Terrific post thoroughly enjoyed reading the blog and more over found to be the tremendous one. In fact, educating the participants with it's amazing content. Hope you share the similar content consecutively.
ReplyDeleteData Analytics Course in Raipur
You completed a number of nice points there. I did a search on the issue and found ExcelR Data Analytics Course nearly all people will have the same opinion with your blog.
ReplyDeleteThanks for posting the best information and the blog is very helpful.data science interview questions and answers
ReplyDeleteI was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur
ReplyDeleteExtremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. python course in delhi
ReplyDeleteAmazing Articles ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteData science course in pune
I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
ReplyDeleteData Analytics Courses In Pune
Impressive blog to be honest definitely this post will inspire many more upcoming aspirants. Eventually, this makes the participants to experience and innovate themselves through knowledge wise by visiting this kind of a blog. Once again excellent job keep inspiring with your cool stuff.
ReplyDeleteData Science Training in Bhilai
Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.
ReplyDeleteData Science Course in Bhilai
Wonderful blog. I am delighted in perusing your articles. This is genuinely an incredible pursuit for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
ReplyDeletedata analytics courses in hyderabad with placements
Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.business analytics course in rohtak
ReplyDelete