Share your knowledge with other learners . . . Write As Guest

Ternary Operator In JavaScript In Hindi | JavaScript Conditional Operator In Hindi

Top Blogs

Loading ...


Image could not load

Ternary Operator In JavaScript

JavaScript Ternary Operator

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 करने के लिए किया जाता है।

Ternary Operator Example

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 ..

Give Feedback

like-image 1.

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

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