Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ?: syntax in JavaScript? [duplicate]

Tags:

javascript

what does it mean for the following line?

T = ($("#a .b").hasClass("active") ? "C" : "D") ;

$("#a .b").hasClass("active") means whether #a .b exists? but how about ? "C" : "D", is it some kind of comparison logic?

like image 841
user2381130 Avatar asked Dec 01 '25 00:12

user2381130


1 Answers

It's a ternary operator

condition ? expr1 : expr2 

If condition is true then expr1 would return else expr2 will return.

So, in your case:

T = ($("#a .b").hasClass("active") ? "C" : "D") ;

T variable will hold "C" if $("#a .b") has class active else it would hold "D"

like image 132
Bhojendra Rauniyar Avatar answered Dec 03 '25 14:12

Bhojendra Rauniyar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!