www.tutorialspoint.com
Open in
urlscan Pro
192.229.210.176
Public Scan
URL:
http://www.tutorialspoint.com/javascript/javascript_switch_case.htm
Submission: On April 14 via api from CA — Scanned from CA
Submission: On April 14 via api from CA — Scanned from CA
Form analysis
1 forms found in the DOM<form class="rounded position-relative">
<input class="form-control pe-4 bg-secondary bg-opacity-10 border-0 search-strings" type="search" placeholder="Search tutorials ..." id="search-strings" name="key" data-result="search-results" aria-label="Search">
<button type="button" id="btnSearch" class="btn bg-transparent px-2 py-0 position-absolute top-50 end-0 translate-middle-y"><i class="fal fa-search fs-6"></i></button>
<div class="search-box search-box-inn" id="search-results"></div>
<div class="clear"></div>
</form>
Text Content
* Home * Coding Ground * Jobs * Whiteboard * Tools * Corporate Training Teach with us * * * * * * Login * Category * Academic Tutorials * Big Data & Analytics * Computer Programming * Computer Science * Databases * DevOps * Digital Marketing * Engineering Tutorials * Exams Syllabus * Famous Monuments * GATE Exams * Latest Technologies * Machine Learning * Mainframe Development * Management Tutorials * Mathematics Tutorials * Microsoft Technologies * Misc tutorials * Mobile Development * Java Technologies * Python Technologies * SAP Tutorials * Programming Scripts * Selected Reading * Software Quality * Soft Skills * Telecom Tutorials * UPSC IAS Exams * Web Development * Sports Tutorials * XML Technologies * Multi-Language * Interview Questions * Prime Packs * Courses * eBooks * Library * Articles Login * * Javascript Basics Tutorial * Javascript - Home * Javascript - Overview * Javascript - Syntax * Javascript - Enabling * Javascript - Placement * Javascript - Variables * Javascript - Operators * Javascript - If...Else * Javascript - Switch Case * Javascript - While Loop * Javascript - For Loop * Javascript - For...in * Javascript - Loop Control * Javascript - Functions * Javascript - Events * Javascript - Cookies * Javascript - Page Redirect * Javascript - Dialog Boxes * Javascript - Void Keyword * Javascript - Page Printing * JavaScript Objects * Javascript - Objects * Javascript - Number * Javascript - Boolean * Javascript - Strings * Javascript - Arrays * Javascript - Date * Javascript - Math * Javascript - RegExp * Javascript - HTML DOM * JavaScript Advanced * Javascript - Error Handling * Javascript - Validations * Javascript - Animation * Javascript - Multimedia * Javascript - Debugging * Javascript - Image Map * Javascript - Browsers * JavaScript Useful Resources * Javascript - Questions And Answers * Javascript - Quick Guide * Javascript - Functions * Javascript - Resources * Selected Reading * UPSC IAS Exams Notes * Developer's Best Practices * Questions and Answers * Effective Resume Writing * HR Interview Questions * Computer Glossary * Who is Who JAVASCRIPT - SWITCH CASE -------------------------------------------------------------------------------- Previous Page Next Page You can use multiple if...else…if statements, as in the previous chapter, to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable. Starting with JavaScript 1.2, you can use a switch statement which handles exactly this situation, and it does so more efficiently than repeated if...else if statements. FLOW CHART The following flow chart explains a switch-case statement works. SYNTAX The objective of a switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used. switch (expression) { case condition 1: statement(s) break; case condition 2: statement(s) break; ... case condition n: statement(s) break; default: statement(s) } The break statements indicate the end of a particular case. If they were omitted, the interpreter would continue executing each statement in each of the following cases. We will explain break statement in Loop Control chapter. EXAMPLE Try the following example to implement switch-case statement. Live Demo <html> <body> <script type = "text/javascript"> <!-- var grade = 'A'; document.write("Entering switch block<br />"); switch (grade) { case 'A': document.write("Good job<br />"); break; case 'B': document.write("Pretty good<br />"); break; case 'C': document.write("Passed<br />"); break; case 'D': document.write("Not so good<br />"); break; case 'F': document.write("Failed<br />"); break; default: document.write("Unknown grade<br />") } document.write("Exiting switch block"); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> OUTPUT Entering switch block Good job Exiting switch block Set the variable to different value and then try... Break statements play a major role in switch-case statements. Try the following code that uses switch-case statement without any break statement. Live Demo <html> <body> <script type = "text/javascript"> <!-- var grade = 'A'; document.write("Entering switch block<br />"); switch (grade) { case 'A': document.write("Good job<br />"); case 'B': document.write("Pretty good<br />"); case 'C': document.write("Passed<br />"); case 'D': document.write("Not so good<br />"); case 'F': document.write("Failed<br />"); default: document.write("Unknown grade<br />") } document.write("Exiting switch block"); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> OUTPUT Entering switch block Good job Pretty good Passed Not so good Failed Unknown grade Exiting switch block Set the variable to different value and then try... Previous Page Print Page Next Page Advertisements ANNUAL MEMBERSHIP Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses Subscribe Now TRAINING FOR A TEAM Affordable solution to train a team and make them project ready. Submit Demo Request * About us * Refund Policy * Terms of use * Privacy Policy * FAQ's * Contact © Copyright 2023. All Rights Reserved. We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more