JS Reference

JS by Category JS by Alphabet

JavaScript

JS Array JS Boolean JS Classes JS Date JS Error JS Global JS JSON JS Math JS Number JS Operators JS RegExp JS Statements JS String

Window

Window Object Window Console Window History Window Location Window Navigator Window Screen

HTML DOM

DOM Document DOM Element DOM Attributes DOM Events DOM Event Objects DOM HTMLCollection DOM Style
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundColor backgroundImage backgroundPosition backgroundRepeat backgroundClip backgroundOrigin backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight borderRightColor borderRightStyle borderRightWidth borderSpacing borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius borderTopStyle borderTopWidth borderWidth bottom boxShadow boxSizing captionSide caretColor clear clip color columnCount columnFill columnGap columnRule columnRuleColor columnRuleStyle columnRuleWidth columns columnSpan columnWidth counterIncrement counterReset cursor direction display emptyCells filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap cssFloat font fontFamily fontSize fontStyle fontVariant fontWeight fontSizeAdjust height isolation justifyContent left letterSpacing lineHeight listStyle listStyleImage listStylePosition listStyleType margin marginBottom marginLeft marginRight marginTop maxHeight maxWidth minHeight minWidth objectFit objectPosition opacity order orphans outline outlineColor outlineOffset outlineStyle outlineWidth overflow overflowX overflowY padding paddingBottom paddingLeft paddingRight paddingTop pageBreakAfter pageBreakBefore pageBreakInside perspective perspectiveOrigin position quotes resize right scrollBehavior tableLayout tabSize textAlign textAlignLast textDecoration textDecorationColor textDecorationLine textDecorationStyle textIndent textOverflow textShadow textTransform top transform transformOrigin transformStyle transition transitionProperty transitionDuration transitionTimingFunction transitionDelay unicodeBidi userSelect verticalAlign visibility width wordBreak wordSpacing wordWrap widows zIndex

Web APIs

API Console API Fullscreen API Geolocation API History API MediaQueryList API Storage

HTML Objects

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

Other References

CSSStyleDeclaration JS Conversion


जावास्क्रिप्ट अगर... और

उदाहरण

यदि घंटा 20 से कम है, तो आउटपुट "गुड डे":

let hour = new Date().getHours();
if (hour < 20) {
  document.getElementById("demo").innerHTML = "Good day";
}

आउटपुट "गुड डे" या "गुड इवनिंग":

let hour = new Date().getHours();
if (hour < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

नीचे और अधिक उदाहरण।


परिभाषा और उपयोग

यदि निर्दिष्ट शर्त सत्य है तो if/else कथन कोड के ब्लॉक को निष्पादित करता है। यदि शर्त गलत है, तो कोड का एक और ब्लॉक निष्पादित किया जा सकता है।

if/else स्टेटमेंट जावास्क्रिप्ट के "सशर्त" स्टेटमेंट्स का एक हिस्सा है, जिसका इस्तेमाल अलग-अलग परिस्थितियों के आधार पर अलग-अलग एक्शन करने के लिए किया जाता है।

जावास्क्रिप्ट में हमारे पास निम्नलिखित सशर्त कथन हैं:

  • यदि निर्दिष्ट शर्त सत्य है, तो निष्पादित किए जाने वाले कोड के ब्लॉक को निर्दिष्ट करने के लिए if का उपयोग करें
  • निष्पादित किए जाने वाले कोड के ब्लॉक को निर्दिष्ट करने के लिए अन्य का उपयोग करें , यदि वही स्थिति गलत है
  • यदि पहली शर्त गलत है, तो परीक्षण के लिए एक नई शर्त निर्दिष्ट करने के लिए अन्य का उपयोग करें
  • निष्पादित किए जाने वाले कोड के कई ब्लॉकों में से एक का चयन करने के लिए स्विच का उपयोग करें

वाक्य - विन्यास

यदि कोई शर्त सत्य है तो if कथन निष्पादित किए जाने वाले कोड के ब्लॉक को निर्दिष्ट करता है:

if (condition) {
  // block of code to be executed if the condition is true
}

अन्य कथन कोड के एक ब्लॉक को निष्पादित करने के लिए निर्दिष्ट करता है यदि स्थिति गलत है :

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

अन्य यदि कथन एक नई शर्त निर्दिष्ट करता है यदि पहली शर्त गलत है:

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

पैरामीटर मान

Parameter Description
condition Required. An expression that evaluates to true or false


और ज्यादा उदाहरण

यदि समय 10:00 से कम है, तो "गुड मॉर्निंग" ग्रीटिंग बनाएं, यदि नहीं, लेकिन समय 20:00 से कम है, तो "गुड डे" ग्रीटिंग बनाएं, अन्यथा "गुड इवनिंग":

var time = new Date().getHours();
if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

यदि दस्तावेज़ में पहले <div> तत्व में "myDIV" की एक आईडी है, तो उसका फ़ॉन्ट-आकार बदलें:

var x = document.getElementsByTagName("DIV")[0];

if (x.id === "myDIV") {
  x.style.fontSize = "30px";
}

यदि उपयोगकर्ता छवि पर क्लिक करता है, तो <img> तत्व के स्रोत विशेषता (src) का मान बदलें:

<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">

<script>
function changeImage() {
  var image = document.getElementById("myImage");
  if (image.src.match("bulbon")) {
    image.src = "pic_bulboff.gif";
  } else {
    image.src = "pic_bulbon.gif";
  }
}
</script>

उपयोगकर्ता इनपुट के आधार पर एक संदेश प्रदर्शित करें:

var letter = document.getElementById("myInput").value;
var text;

// If the letter is "c"
if (letter === "c") {
  text = "Spot on! Good job!";

// If the letter is "b" or "d"
} else if (letter === "b" || letter === "d") {
  text = "Close, but not close enough.";

// If the letter is anything else
} else {
  text = "Waaay off..";
}

इनपुट डेटा मान्य करें:

var x, text;

// Get the value of the input field with id="numb"
x = document.getElementById("numb").value;

// If x is Not a Number or less than 1 or greater than 10, output "input is not valid"
// If x is a number between 1 and 10, output "Input OK"

if (isNaN(x) || x < 1 || x > 10) {
  text = "Input not valid";
} else {
  text = "Input OK";
}

संबंधित पृष्ठ

जावास्क्रिप्ट ट्यूटोरियल: जावास्क्रिप्ट यदि...अन्य कथन

जावास्क्रिप्ट ट्यूटोरियल: जावास्क्रिप्ट स्विच स्टेटमेंट


ब्राउज़र समर्थन

if...else एक ECMAScript1 (ES1) सुविधा है।

ES1 (जावास्क्रिप्ट 1997) सभी ब्राउज़रों में पूरी तरह से समर्थित है:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes