JavaScript switch Loop In Hindi

📔 : Java Script 🔗

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 होता है।

JavaScript switch Syntax

switch (expression) 
{
    case valueN:
       document.write("case 1");
        break;
    case valueN:
        document.write("Case 2");
        break;
    default:
        document.write("default case");
}

  • expression - दिया गया expression , case में दी गयी value से match होता है।
  • case clause - एक switch loop में कितने ही case clause हो सकते हैं , और case में दी जाने वाली value दिए गए expression से match करती है अगर expression match होता है ,तो उस case से associated code of block रन हो जाता है।
  • break - expression match होने पर उस case से associated code of block रन होने के बाद break statement switch loop को terminate करता है।   learn more about break . . .
  • default - अगर कोई भी expression match नहीं होता है तो default statement run होता है।

JavaScript switch Example

File : switch.html

Copy Fullscreen Close Fullscreen Run
<!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>
Output
x is apple

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

Copy Fullscreen Close Fullscreen Run
<!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>
Output
x is apple
x is orange
default case running

तो देखा आपने कि break keyword remove करने पर जो case match होता है वहाँ से सभी statements (including default statement ) execute होते है।

Note - Example में <br> का use line break करने के लिए किया गया है।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers