JavaScript "new Function" Syntax : JS "new Function" Syntax
Promise In JavaScript In Hindi | JS Promise constructor
Prmise.all() Vs Promise.allSettled() | JS allSettled() Example
JavaScript console Object : Window Console Object In Hindi
Syntax Error In JavaScript In Hindi | JS SyntaxError
What is DHCP In Hindi? DHCP kya hai?
Java program to check if string is a palindrome : check palindrome string in java
Java program to check if a number is Armstrong : Find Armstrong Number In Java
Snowlake UDF Example | User-Defined Functions In SNowflake In Hindi
Laravel Service Container Example | Service Container In Laravel 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.
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 ..
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