JavaScript If Else conditions के according किसी Code Of Block को रन करने के लिए use होता है।  Means जब हमें Condition True होने पर कोई दूसरा code run करना हो Condition गलत होने पर कुछ और तब हम If Else का use करते हैं।


JavaScript में If Else की कुछ forms नीचे define की गयी हैं।

  • If Statement
  • If Else Statement
  • If Else If (or Else if) Statement

JavaScript If Statement

If statement , सबसे basic decision making statement है जिसमें किसी particular condition के true होने पर ही code of block रन होता है।

JavaScript If Example

Another File : if.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
 <head>
    <title>JavaScript If Statement</title>
 </head>
<body>
 <script>
    let age  = 19;
    if(age >= 18)
    {
      document.write('You are eligible to vote');
    }
 </script>
 </body>
</html>
Output
You are eligible to vote

JavaScript में empty string , 0 , false , null , undefined, empty array हमेशा false ही होते हैं , इसके अलावा string (except empty string) , object , array (that have some value), defined variable (जिसकी value empty string , 0 , false , null नहीं है) हमेशा true होते हैं। इसलिए If else condition में use होने वाली value / expression की जगह हम इन्हे भी use कर सकते हैं।


JavaScript If Else

जब हमें Condition True होने पर कोई दूसरा code run करना हो Condition गलत होने पर कुछ और तब हम If Else का use करते हैं।

JavaScript If Else Example

Another File : ifelse.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
 <head>
    <title>JavaScript If Else Statement</title>
 </head>
<body>
 <script>
    let age  = 12;
    if(age >= 18)
    {
      document.write('You are eligible to vote');
    }
    else
    {
       document.write('You are not eligible to vote');
    }
 </script>
 </body>
</html>
Output
You are not eligible to vote

JavaScript If Else If

ऊपर दिए गए दोनों statements (if और if else) को हम तब use करते हैं जब कोई single condition के accordingly code of block run करना हो , एक से ज्यादा condition के accordingly code of block run के लिए हम If Else If Statement use करते हैं।

JavaScript If Else If Example

Another File : ifelseif.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript If Else If Statement</title>
  </head>
  <body>
   <script>
     let marks  = 86;
     if(marks >= 90)
     {
        document.write('Grade : A+');
      }
      else if(marks >= 80)
      {
       document.write('Grade : A');
     }
     else if(marks >= 70)
     {
       document.write('Grade : B');
     }
     else
     {
       document.write('Grade : C');
     }
    </script>
  </body>
</html>
Output
Grade : A

Note - if else if में else और if के बीच space जरूर होना चाहिए इसे आप elseif नहीं लिख सकते हैं।

इसके अलावा आप if या else के अंदर भी if / Else statement लिख सकते हैं जिसे nested if else कहते हैं।

जैसा कि आप पिछले topics में भी पढ़ चुके होंगे कि JavaScript Case Sensitive Language है इसलिए आप ये ध्यान रखें की if / else small letter में ही लिखें।

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