← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 2 of 10

JS Assignment

Imagine adjusting a thermostat, instead of always typing the full new target temperature, you can just tap a plus or minus button that adjusts the current setting by a step. JavaScript's compound assignment operators work the same way, instead of writing count = count + 1, you can write the shorter count += 1, adjusting a variable's existing value directly. These shortcuts show up constantly in real code, including the scripts powering interactive features across cookiescursor.com, so recognizing them quickly is a genuinely useful skill.
Syntax
javascript
variable = value;
variable += value;  // also -=, *=, /=, %=, **=

बुनियादी Compound Assignment

+=, -=, *= और /= हर एक किसी arithmetic operation को assignment के साथ जोड़ता है, और variable को उसी के वर्तमान value के आधार पर एक अकेले, संक्षिप्त statement में अपडेट करता है।

Note: Compound assignment operators स्वाभाविक रूप से 'variable बनता है variable operator value' की तरह पढ़े जाते हैं, जो उन्हें पहली बार parse करना सीखते समय मदद करता है।
Warning: Compound assignment requires the variable to already exist and hold a value, it cannot be used as the very first declaration.

उदाहरण: Basic Compound Assignment

javascript
// 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 जैसे ही संक्षिप्त पैटर्न का पालन करते हुए।

Note: %= किसी counter को सीमा तक पहुँचने पर वापस शून्य पर लपेटने के लिए काम का है, जो घूमते displays या carousels में आम पैटर्न है।
Warning: **= with a fractional exponent produces a decimal result, which is easy to overlook if you expect only whole numbers.

उदाहरण: Remainder and Exponent Assignment

javascript
// 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 को केवल तभी अपडेट करने के लिए उपयोगी है जब वह सार्थक रूप से मौजूद हो।

Note: &&= को ऐसे सोचिए जैसे कह रहा हो 'इसे तभी अपडेट करो जब इसमें अभी कुछ हो'।
Warning: &&= will not assign anything if the current value is falsy, including 0 or an empty string, which can surprise developers expecting it to always run.

उदाहरण: Logical AND Assignment (&&=)

javascript
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 को अधिलेखित कर सकता है जिन्हें आप रखना चाहते थे, और ठीक इसी कारण ??= मौजूद है।

Note: ||= किसी भी falsy value के लिए बैकअप default भरने का संक्षिप्त तरीका है, जिनमें 0, खाली strings और undefined शामिल हैं।
Warning: Because || treats 0 and empty strings as falsy, ||= will overwrite an intentionally set 0 or '', which is not always the desired behavior.

उदाहरण: Logical OR Assignment (||=)

javascript
let username = "";
username ||= "Guest";
console.log(username); // "Guest", "" is falsy

Nullish Coalescing Assignment (??=)

??= नया value तभी assign करता है जब variable अभी null या undefined हो, ||= के विपरीत, यह 0 और खाली strings जैसे अन्य falsy values को वैध मानता है और उन्हें अछूता छोड़ता है।

Note: ??= को ||= के मुक़ाबले खास तौर पर तब तरजीह दीजिए जब 0 या खाली string को वैध, इरादतन value गिना जाना चाहिए जिसे अधिलेखित नहीं करना है।
Warning: Confusing ??= with ||= is easy since they look similar, but they behave differently for falsy-but-not-nullish values like 0.

उदाहरण: Nullish Coalescing Assignment (??=)

javascript
let count = 0;
count ??= 10;
console.log(count); // 0 stays, only null/undefined trigger ??=

let missing;
missing ??= 10;
console.log(missing); // 10
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. #}
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

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.