← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 3 of 26

JS कंडीशनल्स

किसी border crossing checkpoint के बारे में सोचें, अगर आपके पास valid passport है, तो आपको जाने दिया जाता है, नहीं तो आपको किसी दूसरी line में भेज दिया जाता है, नतीजा हमेशा पहले एक खास condition जांचने पर निर्भर करता है। JavaScript में conditionals भी बिल्कुल वैसे ही काम करते हैं, ये आपके कोड को यह जांचने देते हैं कि कुछ true है या नहीं, और जवाब के हिसाब से if, else if, और else का इस्तेमाल करके अलग-अलग instructions चलाते हैं। लगभग हर interactive feature कहीं न कहीं conditionals पर निर्भर करता है, चाहे वह login status जांचना हो या cookiescursor.com पर कोई personalized message दिखाना हो।
Syntax
javascript
if (condition1) {
  // code
} else if (condition2) {
  // code
} else {
  // code
}

if Statement

एक if statement अपना code block तभी चलाता है जब उसके parentheses के अंदर की condition true evaluate होती है, और अगर condition false हो तो पूरे block को skip कर देता है।

Note: कोई भी value condition के रूप में आ सकती है, JavaScript उसे truthy या falsy होने के आधार पर अपने आप true या false में बदल देता है।
Warning: किसी multi-line if body के आसपास curly braces न लगाना सिर्फ अगले single statement को conditional बनाता है, बाद की lines बिना शर्त के चलती हैं।

उदाहरण: The if Statement

javascript
// Declare the constant `age`, set to `20`
// Declare the constant `age`, set to `20`
const age = 20;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
  // Print "You are an adult." to the console
  // Print "You are an adult." to the console
  console.log("You are an adult.");
}

else

एक else block तभी चलता है जब पहले वाली if condition false रही हो, जो एक fallback path देता है ताकि दोनों में से हमेशा ठीक एक block चले।

एक else हमेशा उससे पहले आने वाले nearest unmatched if से जुड़ा होता है, जो conditionals nested होने पर मायने रखता है।

Note: else को खासतौर पर एक genuine alternative outcome के लिए इस्तेमाल करें, न कि सिर्फ ऐसे unrelated code को रखने की जगह जिसे हमेशा चलना ही है।
Warning: एक else block को अपने matching if block के ठीक बाद आना चाहिए, बीच में दूसरे statements डालने से यह connection टूट जाता है।

उदाहरण: else

javascript
// Declare the constant `age`, set to `15`
// Declare the constant `age`, set to `15`
const age = 15;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
  // Print "Adult" to the console
  // Print "Adult" to the console
  console.log("Adult");
// Otherwise, run this branch
// Otherwise, run this branch
} else {
  // Print "Minor" to the console
  // Print "Minor" to the console
  console.log("Minor");
}

else if

else if शुरुआती if के बाद क्रम में और conditions जांचने देता है, और जिस भी पहले block की condition true evaluate होती है वहीं रुककर उसे चलाता है, जो तब उपयोगी है जब दो से ज़्यादा संभावित outcomes हों।

Note: अपनी else if conditions को सबसे specific से लेकर सबसे कम specific के क्रम में रखें, क्योंकि JavaScript पहली मैच होने वाली condition पर ही रुक जाता है।
Warning: एक else if chain हमेशा ज़्यादा से ज़्यादा एक ही block चलाती है, भले ही technically कई conditions true हों, सिर्फ पहला match ही execute होता है।

उदाहरण: else if

javascript
// Declare the constant `score`, set to `75`
// Declare the constant `score`, set to `75`
const score = 75;
// Check whether `score >= 90`
// Check whether `score >= 90`
if (score >= 90) {
  // Print "A" to the console
  // Print "A" to the console
  console.log("A");
// Otherwise, if `score >= 70`, run this branch
// Otherwise, if `score >= 70`, run this branch
} else if (score >= 70) {
  // Print "B" to the console
  // Print "B" to the console
  console.log("B");
// Otherwise, run this branch
// Otherwise, run this branch
} else {
  // Print "C" to the console
  // Print "C" to the console
  console.log("C");
}

Nested if Statements

एक if statement को दूसरे if statement के block के अंदर रखा जा सकता है, जिससे आप पहली condition के confirm true होने के बाद ही दूसरी condition जांच सकते हैं।

Note: जब nesting बहुत गहरी लगने लगे, तो एक ही if के अंदर && से conditions को मिलाना अक्सर कई nested levels से ज़्यादा साफ होता है।
Warning: बहुत गहराई तक nested if statements जल्दी ही पढ़ना मुश्किल हो सकते हैं, नेस्टिंग बहुत ज़्यादा होने पर उन्हें अलग functions या combined conditions में refactor करने पर विचार करें।

उदाहरण: Nested if Statements

javascript
// Declare the constant `age`, set to `25`
// Declare the constant `age`, set to `25`
const age = 25;
// Declare the constant `hasLicense`, set to `true`
// Declare the constant `hasLicense`, set to `true`
const hasLicense = true;
// Check whether `age >= 18`
// Check whether `age >= 18`
if (age >= 18) {
  // Check whether `hasLicense`
  // Check whether `hasLicense`
  if (hasLicense) {
    // Print "Can drive" to the console
    // Print "Can drive" to the console
    console.log("Can drive");
  }
}

Ternary Operator और Short Circuit Evaluation

Ternary operator, condition ? valueIfTrue : valueIfFalse, एक साधारण if/else का एक compact विकल्प है, और short circuit evaluation && को पहली falsy value पर और || को पहली truthy value पर रुकने देता है, बाकी को जांचे बिना।

Note: && के साथ short circuit evaluation किसी value के मौजूद होने पर ही code चलाने के लिए एक आम pattern है, जैसे userData && userData.name।
Warning: Ternary operators साधारण value selection के लिए सबसे अच्छे हैं, side effects वाली जटिल logic के लिए इनका इस्तेमाल करना code को समझना कहीं ज़्यादा मुश्किल बना देता है।

उदाहरण: Ternary Operator and Short Circuit Evaluation

javascript
// Declare the constant `age`, set to `20`
// Declare the constant `age`, set to `20`
const age = 20;
// Declare the constant `status`, set to `age >= 18 ? "adult" : "minor"`
// Declare the constant `status`, set to `age >= 18 ? "adult" : "minor"`
const status = age >= 18 ? "adult" : "minor";
// Print `status` to the console
// Print `status` to the console
console.log(status);
// Print `true || console.log("never runs")` to the console
// Print `true || console.log("never runs")` to the console
console.log(true || console.log("never runs"));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी multi-statement if block के चारों ओर curly braces भूल जाना, जिससे सिर्फ अगली line conditional होती है, पूरा intended block नहीं।
  2. किसी condition के अंदर === के बजाय single = इस्तेमाल करना, जो value compare करने के बजाय चुपचाप एक value assign कर देता है।
  3. nested if statements की बहुत गहरी chains लिखना जब एक साफ else if chain, या यहां तक कि एक switch, ज़्यादा readable होता।
चैप्टर सारांश
  • if किसी block को सिर्फ तब चलाता है जब उसकी condition true हो, और else false होने पर एक fallback देता है।
  • else if क्रम में कई conditions check करने देता है, पहली true वाली पर रुकते हुए।
  • Ternary operator simple two-outcome conditionals के लिए एक compact form देता है, और short circuit evaluation && और || को अनावश्यक checks skip करने देता है।
ब्राउज़र सपोर्ट

if, else if, else, और ternary operator core JavaScript syntax हैं जो हर browser में समान रूप से supported हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.