JS असाइनमेंट
In this page:
variable = value;
variable += value; // also -=, *=, /=, %=, **=
बुनियादी Compound Assignment
+=, -=, *= और /= हर एक किसी arithmetic operation को assignment के साथ जोड़ता है, और variable को उसी के वर्तमान value के आधार पर एक अकेले, संक्षिप्त statement में अपडेट करता है।
उदाहरण: Basic Compound Assignment
// Declare the variable `total`, set to `10`
// Declare the variable `total`, set to `10`
let total = 10;
total += 5;
total -= 2;
total *= 2;
total /= 4;
// Print `total` to the console
// Print `total` to the console
console.log(total);
Remainder और Exponent Assignment
%= शेषफल की क्रिया लगाकर नतीजे को वापस assign करता है, और **= exponentiation लगाकर नतीजे को वापस assign करता है, दूसरे compound operators जैसे ही संक्षिप्त पैटर्न का पालन करते हुए।
उदाहरण: Remainder and Exponent Assignment
// Declare the variable `counter`, set to `7`
// Declare the variable `counter`, set to `7`
let counter = 7;
counter %= 3;
// Print `counter` to the console
// Print `counter` to the console
console.log(counter);
// Declare the variable `base`, set to `2`
// Declare the variable `base`, set to `2`
let base = 2;
base **= 3;
// Print `base` to the console
// Print `base` to the console
console.log(base);
Logical AND Assignment (&&=)
&&= किसी variable को नया value तभी assign करता है जब वह variable अभी truthy हो, और अगर वह पहले से falsy है तो उसे बदला नहीं छोड़ता, जो किसी value को केवल तभी अपडेट करने के लिए उपयोगी है जब वह सार्थक रूप से मौजूद हो।
उदाहरण: Logical AND Assignment (&&=)
let session = "active";
session &&= "updated";
console.log(session); // "updated"
let empty = "";
empty &&= "updated";
console.log(empty); // "" stays unchanged
Logical OR Assignment (||=)
||= नया value तभी assign करता है जब variable अभी falsy हो, जो आमतौर पर किसी ऐसी चीज़ के लिए default value देने में इस्तेमाल होता है जो ग़ायब, खाली या शून्य हो सकती है।
क्योंकि 0 और '' भी falsy हैं, ||= उन values को अधिलेखित कर सकता है जिन्हें आप रखना चाहते थे, और ठीक इसी कारण ??= मौजूद है।
उदाहरण: Logical OR Assignment (||=)
let username = "";
username ||= "Guest";
console.log(username); // "Guest", "" is falsy
Nullish Coalescing Assignment (??=)
??= नया value तभी assign करता है जब variable अभी null या undefined हो, ||= के विपरीत, यह 0 और खाली strings जैसे अन्य falsy values को वैध मानता है और उन्हें अछूता छोड़ता है।
उदाहरण: Nullish Coalescing Assignment (??=)
let count = 0;
count ??= 10;
console.log(count); // 0 stays, only null/undefined trigger ??=
let missing;
missing ??= 10;
console.log(missing); // 10
- count += 1 के बजाय count =+ 1 लिखना, space और operator order मायने रखता है, =+ असल में assignment के बाद unary plus है।
- ||= चाहते हुए ??= इस्तेमाल करना, ?? सिर्फ null और undefined को missing मानता है, जबकि || किसी भी falsy value को missing मानता है।
- यह भूल जाना कि compound assignment operators variable को in place modify करते हैं, वे एक नया variable नहीं बनाते।
- +=, -=, *=, /=, और %= जैसे compound operators एक operation को assignment के साथ एक step में combine करते हैं।
- **= exponentiation और assignment को साथ लागू करता है, बाकी compound operators जैसे ही pattern follow करते हुए।
- &&=, ||=, और ??= सिर्फ तब value conditionally assign करते हैं जब current value के बारे में कुछ logical conditions पूरी हों।
Standard compound assignment operators हर browser में काम करते हैं; &&=, ||=, और ??= modern additions हैं जो सभी current browser versions में supported हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: