3

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?

2
  • It's the ternary operator (has nothing to do with jQuery) and is shorthand for an if/else statement Commented Jul 27, 2015 at 4:35
  • You are correct, I just saw it in a jquery statement. Thanks Roy Commented Jul 27, 2015 at 4:44

2 Answers 2

4

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"

Sign up to request clarification or add additional context in comments.

Comments

1

It tell you that :

if ( $("#a .b").hasClass("active") ) {    
   T = "C";
} else {
   T = "D"
}

Here you can read this doc for further understanding. Ternary Operator

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.