Optional chaining in JavaScript In Hindi | JS Optional chaining In Hindi
JavaScript Hoisting In Hindi | Hoisting In JavScript
JavaScript Spread syntax In Hindi : Spread Operator In JavaScript
Promise In JavaScript In Hindi | JS Promise constructor
JavaScript Interview Question For Beginners In Hindi
Linux Commands For Begginers | Linux Commands In Hindi - Part 2
Best Laptops Under 50k On Amazon In 2023
Computer Basic Knowledge in Hindi
AWS S3-Snowflake Pipeline Example | Snowflake Tutorials In Hindi
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript में Nullish Coalescing Operator भी एक Logical Operator है जिसका main purpose default value use करना था। Basically यह सिर्फ दो vales (null
, undefined
) ही check करता है , अगर किसी variable की value null / undefined हुई तो यह default value return करेगा otherwise वो variable की actual value ही मिलेगी।
Nullish Coalescing Operator को ES2020 में add किया गया था , और इसे इसे double question mark ( ??
) से represent किया जाता है।
Example
Suppose कीजिये हमारे पास दो variables a
, b
हैं तो operator को कुछ इस तरह से use करेंगे।
let result = a ?? b ;
अब अगर variable a की value null/undefined हुई तो b return होगा otherwise आपको a ही return होगा।
नीचे दिए गए example को ध्यान से देखें और समझे।
console.log(10 ?? "default value");
console.log(0 ?? "default value");
console.log(null ?? "default value");
Output
10 0 default value
जैसा कि आपको पता है कि AND
, OR
operator के साथ use की गयी values का type boolean नहीं है तो पहले वो values internally boolean value में Type Cast होगीं फिर condition के according evaluation होगा। और type casting के बाद अगर value true है तो वो actual value return होगी।
और अगर आप 0 , empty string "", false को type cast करोगे तो आपको false
ही मिलेगा ऐसे में अगर हमें 0 या false को true
consider करना हो तो हम OR
operator के साथ नहीं कर पाएंगे।
For Example
// case 1.
let marks = 0;
console.log(marks || 100);
// case 2.
let pass = false;
console.log(pass || "true");
output
100 true
अब same example को अगर आप Nullish Coalescing Operator की help से evaluate करेंगे तो result different होगा।
// case 1.
let marks = 0;
console.log(marks ?? 100);
// case 2.
let pass = false;
console.log(pass ?? "true");
Output
0 false
अब आपको ये clear हो गया होगा कि OR
operator से Nullish Coalescing Operator क्यों better है।
Nullish Coalescing Operator को AND , OR operator के साथ थोड़ा ध्यान से use करना होगा इसे आप directly इन operators के साथ use नहीं कर सकते हैं।
For Example
let res = null || undefined ?? "default value";Uncaught SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions
ऐसा Operator Precedence की वजह से होता है।
ऐसी situation से बचने के लिए आपको conditions का pair बन देना चाहिए , मतलब आप हर condition को need के according parenthesis ()
में रख देना चाहिए।
console.log((null || undefined) ?? "default value"); // default value
console.log(null || (undefined ?? "default value")); // default value
I Hope, आपको JavaScript में Nullish Coalescing Operator के बारे में अच्छे से समझ आ गया होगा।
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook