Loading ...
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.
Ternary Operator In JavaScript
JavaScript में ternary operator को conditional operator भी कहते हैं , ये only ऐसा operator है जो 3 operands के साथ work करता है। इससे पहले आपने Logical Operators , Nullish Coalescing Operator या Comparison Operators में देखा होगा कि 1 - 2 operands (value) use में लेते थे। लेकिन ternary operator के साथ 3 operands use किये जाते हैं।
जैसा कि इसका नाम है conditional operator , मतलब इसका use condition के bases पर value को manipulate या change करने के लिए किया जाता है।
Conditional / Ternary Operator में हम ( ? :
) use करते हैं , सबसे पहले दिए गए expression / condition को evaluate करते हैं अगर expression true है तो question mark ( ?
) के बाद का statement run होगा और अगर expression / condition false है तो colon ( :
) के बाद वाला statement run होगा।
for example
let res = a ? b : c;
यहां अगर a
की value true
हुई तो b
return होगा otherwise c
return होगा।
Example
let status = 1;
let res = status == 1 ? "Online" : "Offline";
console.log(res);
Online
normally इसे single line if else की तरह use किया जाता है। same example को आप if else से कुछ इस तरह से कर सकते हैं।
let status = 1;
if(status == 1) {
console.log("Online");
}
else {
console.log("Offline");
}
Online
हालाँकि दोनों का result आपको same मिलेगा , लेकिन अगर आपको single line code ही लिखना है तो ternary operator best है।
Logical Operators की तरह ही इसमें भी अगर first operand boolean value नहीं है तो पहले value Internally Boolean value में Type Cast होती है।
will be continue ..
1.
Loading ...
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook