Nullish Coalescing Operator In JavaScript In Hindi

Image could not load

Nullish Coalescing Operator In JavaScrip

Nullish Coalescing Operator

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 होगा।

Nullish Coalescing Operator Example

नीचे दिए गए example को ध्यान से देखें और समझे।

console.log(10 ?? "default value"); console.log(0 ?? "default value"); console.log(null ?? "default value");

Output

10
0
default value

Difference between Or and Nullish Coalescing Operator

जैसा कि आपको पता है कि 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 है।

Operator Precedence

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 के बारे में अच्छे से समझ आ गया होगा।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

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