If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript में switch statement , किसी matched expression के लिए code of block Run करता है , यह लगभग else if की तरह ही work करता है जहा हम कई सारी Conditions में से True Condition वाला statement ही Run होता था, और अगर एक भी condition match नहीं होती तो else part (default) run होता था।
switch में हम case clause use करते हैं , और जो case expression से match करता है वही statement execute करता है। और कोई case match न होने पर default statement execute होता है।
switch (expression) { case valueN: document.write("case 1"); break; case valueN: document.write("Case 2"); break; default: document.write("default case"); }
File : switch.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript switch loop In Hindi </title>
</head>
<body>
<script>
let x = "apple";
switch (x)
{
case "cake":
document.write("x is cake");
break;
case "orange":
document.write("x is orange");
break;
case "apple":
document.write("x is apple");
break;
default:
document.write("default case running");
}
</script>
</body>
</html>
Note - जैसा कि आप example में देख रहे होंगे कि हर case के end में break keyword का use किया गया है , जिससे कि matched case statement ही run हो अगर हम ये break remove तो जिस case के साथ expression match करता है वहाँ से सभी statements (cases) switch Loop ख़त्म होने तक execute होंगे।
See Another Example
File : switch2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript switch loop In Hindi </title>
</head>
<body>
<script>
let x = "apple";
switch (x)
{
case "cake":
document.write(`x is cake`);
break;
case "apple":
document.write(`x is apple <br>`);
case "orange":
document.write(`x is orange <br>`);
default:
document.write(`default case running`);
}
</script>
</body>
</html>
तो देखा आपने कि break keyword remove करने पर जो case match होता है वहाँ से सभी statements (including default statement ) execute होते है।
Note - Example में <br> का use line break करने के लिए किया गया है।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook