← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 7 of 12

JS लॉजिकल असाइनमेंट

Logical assignment operators किसी variable को तभी बदलते हैं जब उसके बारे में कोई condition पूरी हो, जैसे किसी cup को सिर्फ तभी दोबारा भरना जब वह खाली हो। ये जांचने और सेट करने को एक छोटे step में मिला देते हैं।
Syntax
javascript
a ||= b;  // assign if a is falsy
a &&= b;  // assign if a is truthy
a ??= b;  // assign if a is null/undefined

AND Assignment

&&= दाईं तरफ की value सिर्फ तभी assign करता है जब variable अभी truthy हो — a && (a = b) के बराबर, यह किसी value को सिर्फ तभी update करने के लिए उपयोगी है जब वह पहले से मौजूद और सार्थक हो।

उदाहरण: AND Assignment

javascript
// Declare the variable `session`, set to "active"
// Declare the variable `session`, set to "active"
let session = "active";
session &&= "updated";
// Print `session` to the console
// Print `session` to the console
console.log(session);

OR Assignment

||= सिर्फ तभी assign करता है जब variable अभी falsy हो — a || (a = b) के बराबर, यह तब एक default सेट करने का एक compact तरीका है जब किसी variable को अभी तक कोई असली value न मिली हो, हालांकि यह 0 या खाली string जैसी असली falsy values को भी overwrite कर देता है।

उदाहरण: OR Assignment

javascript
// Declare the variable `name`, set to ""
// Declare the variable `name`, set to ""
let name = "";
name ||= "Guest";
// Print `name` to the console
// Print `name` to the console
console.log(name);

Nullish Assignment

??= सिर्फ तभी assign करता है जब variable अभी null या undefined हो — a ?? (a = b) के बराबर, 0 जैसी असली falsy values को छुए बिना defaults सेट करने के लिए यह तीनों में सबसे सुरक्षित है।

उदाहरण: Nullish Assignment

javascript
let count = 0;
count ??= 10;
console.log(count); // 0, unaffected

Practical Defaults

ये operators एक line में गायब object properties या config defaults भरने के लिए खासतौर पर काम आते हैं, assignment से पहले एक explicit if-check लिखने के बजाय।

उदाहरण: Practical Defaults

javascript
// Declare the constant `config`, set to `{}`
// Declare the constant `config`, set to `{}`
const config = {};
config.timeout ??= 3000;
// Print `config.timeout` to the console
// Print `config.timeout` to the console
console.log(config.timeout);

सही Operator चुनना

यह चुनें कि 'गायब' किसे माना जाए: ??= इस्तेमाल करें जब सिर्फ null/undefined को ही default trigger करना चाहिए, ||= जब कोई भी falsy value करे, और &&= जब आप सिर्फ पहले से सेट value को update करना चाहते हों।

उदाहरण: Choosing the Right Operator

javascript
let a; a ??= 1; console.log(a);   // null/undefined only
let b = 0; b ||= 2; console.log(b); // any falsy
let c = "x"; c &&= "y"; console.log(c); // only if truthy
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. यह उम्मीद करना कि a ||= b हमेशा चलेगा, जबकि यह सिर्फ तब assign करता है जब a falsy हो।
  2. 0 या "" valid values होने पर ||= इस्तेमाल करना, जो उन्हें overwrite कर देता है; ??= इस्तेमाल करें।
  3. किसी const पर logical assignment इस्तेमाल करना, जो एक assignment होने पर TypeError throw करता है।

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.